0

I need to modify the product collection on category page. Therefore, I've modified the _getProductCollection() in Mage/Catalog/Block/Product/List.php and added an additional filter $this->_productCollection->addAttributeToFilter('entity_id', $prods);

But this works only partly.

The product count in the top bar shows the correct product count (10) but Magento shows 12 products for this category.

printing the variables in catalog/product/list.phtml shows up following result:

$_productCollection=$this->getLoadedProductCollection();
echo $_productCollection->count() // 12 [wrong]
echo $_productCollection->getSize() // 10 [correct]
echo $_productCollection->getSelect() // Running in db: 10 [correct]
iterating through the products with foreach ($_productCollection as $_product) shows 12 [wrong]

What am I doing wrong? Thanks!

1

Try to clear the product collection by adding following to your list.phtml, right after you load the collection.

$_productCollection->clear();

This should resolve your issue

Note

I recommend to overwrite the Block rather than copy it to app/code/local/Mage

To do so, just create your little extension

Your/Extension/etc/config.xml

<catalog>
    <rewrite>
        <product_list>Your_Extension_Block_Product_List</product_list>
    </rewrite>
</catalog>

Your/Extension/Block/Product/List.php

class Your_Extension_Block_Product_List extends Mage_Catalog_Block_Product_List 
{
    protected function _getProductCollection()
    {
        $customCollection = parent::_getProductCollection();

        // Your logic

        $this->_productCollection->addAttributeToFilter('entity_id', $prods);

        return $this->_productCollection;

    }
}
| improve this answer | |

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.