Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
862 views
in Technique[技术] by (71.8m points)

magento - Magento1.9 Custom collection page add layered navigation

I know Magento1.9 is really, really old, but some people still insist on using it... So, here is my question...

I have a module that creates a new product collection based on some POST parameters and renders them on a custom page. What I want now is to also add layered navigation to that page. However, my attempts so far have been unsuccessful...

Here is my code so far:

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <ID_SearchResults>
            <version>0.1.0</version>
        </ID_SearchResults>
    </modules>
    <frontend>
        <routers>
            <searchresults>
                <use>standard</use>
                    <args>
                        <module>ID_SearchResults</module>
                        <frontName>searchresults</frontName>
                    </args>
            </searchresults>
        </routers>
        <layout>
            <updates>
            <searchresults>
                <file>searchresults.xml</file>
            </searchresults>
            </updates>
        </layout>
    </frontend>
    <global>
        <helpers>
            <id_searchresults>
                <class>ID_SearchResults_Helper</class>
            </id_searchresults>
        </helpers>
        <models>
            <searchresults>
                <class>ID_SearchResults_Model</class>
            </searchresults>
        </models>
    <blocks>
        <searchresults>
            <class>ID_SearchResults_Block</class>
        </searchresults>
    </blocks>
    </global>
</config>

IndexController.php

<?php

class ID_SearchResults_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        $post_params = Mage::app()->getRequest()->getPost();

        $this->loadLayout();
        $this->getLayout()->getBlock("head")->setTitle( $this->__("Search results for: '%s'", $post_params['search']) );
        $breadcrumbs = $this->getLayout()->getBlock("breadcrumbs");
        $breadcrumbs->addCrumb("home", array(
            "label" => $this->__("Home"),
            "title" => $this->__("Home"),
            "link"  => Mage::getBaseUrl()
        ));

        $breadcrumbs->addCrumb("titlename", array(
            "label" => $this->__("Search results for: '%s'", $post_params['search']),
            "title" => $this->__("Search results for: '%s'", $post_params['search'])
        ));

        $this->_initLayoutMessages('catalog/session');
        $this->_initLayoutMessages('checkout/session');

        $this->renderLayout();
    }
}

Data.php

<?php

class ID_SearchResults_Helper_Data extends Mage_Core_Helper_Abstract
{

    public function getAvailableOrders()
    {
        return array(
            // attribute name => label to be used
            'price' => $this->__('Price')
        );
    }

    public function getProductCollection()
    {
        $post_params = Mage::app()->getRequest()->getPost();
        $product_ids = array_map(
            function($item) {
                return (int)filter_var($item, FILTER_SANITIZE_NUMBER_INT);
            },
            explode(',', $post_params['sbl_search_data'])
        );
        $layer = Mage::getSingleton('catalog/layer');

        if( $product_ids[0] === 0 ) {
            if( !is_null(Mage::registry('product-ids')) ) {
                $product_ids = Mage::registry('product-ids');
            }
        }

        $product_ids = array(36896,36892,36886);

        if ( count($product_ids) > 0 ) {
            if( is_null(Mage::registry('product-ids')) ) {
                Mage::register('product-ids', $product_ids);
            }
            $collection = Mage::getResourceModel('catalog/product_collection');
            $attributes = Mage::getSingleton('catalog/config')->getProductAttributes();
            $collection->addAttributeToSelect($attributes);
            $collection->addAttributeToFilter('status', 1);
            $collection->addIdFilter($product_ids);

            $this->_productCollection = $collection;

            $layer->setCollection($this->_productCollection);
        } else {
            $productCollection = $layer->getProductCollection();
            $this->_productCollection = $productCollection;
            $layer->setCollection($this->_productCollection);
        }

        return $this->_productCollection;
    }

}

List.php

<?php

class ID_SearchResults_Block_Product_List extends Mage_Catalog_Block_Product_List
{
    protected function _beforeToHtml()
    {
        $toolbar = $this->getToolbarBlock();

        // called prepare sortable parameters
        $collection = $this->getProductCollection();

        // use sortable parameters
        if ($orders = $this->getAvailableOrders()) {
            $toolbar->setAvailableOrders($orders);
        }
        if ($sort = $this->getSortBy()) {
            $toolbar->setDefaultOrder($sort);
        }
        if ($dir = $this->getDefaultDirection()) {
            $toolbar->setDefaultDirection($dir);
        }
        if ($modes = $this->getModes()) {
            $toolbar->setModes($modes);
        }

        // set collection to toolbar and apply sort
        $toolbar->setCollection($collection);

        $this->setChild('toolbar', $toolbar);

        Mage::dispatchEvent('catalog_block_product_list_collection', array(
            'collection' => $this->getProductCollection()
        ));

        $this->getProductCollection()->load();

        return parent::_beforeToHtml();
    }
}

I am sure there is something obvious I missed here, but it's been so long I since I worked with magento 1.9 that I just can't see it...

Anyone can steer me in the right direction???

thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...