9

In Magento, the Layered Navigation block works nicely in the product list page.

How can this block be successfully copied to a custom page, where there is a custom list of products?

Note: Just adding the block to the XML, and calling it's getChildHtml('blockname') doesn't do the trick.

  • 2
    You need to add a default category to the layared navigation block code as the layared navigation is build from the products of the current category. – Sander Mangel Jan 22 '13 at 21:45
  • +1 for the help, what if there are multiple products from multiple categories (for example, deal products)? – pzirkind Jan 22 '13 at 21:48
  • 2
    I'd make a special category for that. There might be other ways but this is definitely the easiest and most scalable one! – Sander Mangel Jan 22 '13 at 21:50
  • 1
    If you would really want to then you would have to create a custom category. The layered navigation really expects there to be a category to work with, it won't take a simple ProductCollection without overriding a whole lot of classes. – Rick Kuipers Jan 23 '13 at 7:57
5

Basically what you have to do is extend/overwrite from Mage_Catalog_Block_Product_List (List.php) and rewrite the getProductCollection() method:

protected function _getProductCollection()
{
    if (is_null($this->_productCollection)) {
        // Build collection and set it
        $collection = "...";
        $this->setProductCollection($collection);
    }

    return $this->_productCollection;
}

When that was done I had overwritten both the Mage_Catalog_Model_Layer class and the Mage_Catalog_Model_Category and introduced a new variable:

protected $_customProductCollection;

I have overwritten the getProductCollection() in both classes and I added this in the beginning of the method:

if(isset($this->_customProductCollection)){
    return $this->_customProductCollection;
}

I have also a method that allows me to set this "customProductCollection" inside both these classes. Once It's set, the rest of the data of the layered navigation/category is based on this collection.

Check out the initial question+answer here.

Good luck ;)

  • What is this method to set customproductcollection and where do you call it from? – easymoden00b Feb 17 '15 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.