Magento – Fix Filtering in Static Block Not Working

static-block

I added a new attribute 'featured_on_productpage' type yes/no and want to use something like this on one of the cms pages.
I included this
{{block type="catalog/product_list" category_id="7" attribute_code=”featured_on_productpage” attribute_value=”1″ template="catalog/product/list.phtml"}} in the editor of the cms page, but instead of showing only products with the attribute set to 'yes' it shows all products of this category.

What's wrong?

Thanks.

Best Answer

Just so this question would have an answer...
This is not possible out of the box.
you will need to create your own block that extends the catalog/product_list block and handles the additional attributes.

You need to rewrite the _getProductCollection method and make it look like this:

protected function _getProductCollection() 
{
    if (is_null($this->_productCollection)) {
       $this->_productCollection = parent::_getProductCollection(); //do what the parent class does
       //check if you should filter by an attribute
       if ($this->hasData('attribute_code') && $this->hasData('attribute_value')) {
           $this->_productCollection->addAttributeToFilter($this->getData('attribute_code'), $this->getData('attribute_value'));
       }
    }
    return $this->_productCollection();
}

Then you can use your block like this:

{{block type="[module]/product_list" category_id="7" attribute_code=”featured_on_productpage” attribute_value=”1″  template="catalog/product/list.phtml"}}
Related Topic