0

Trying to create a couple of quick extensions, one to display a list of best sellers and one to display a list of items on promotion. I want both of these modules to filter by a given cateogry id if one is supplied. So I found out that magento has a built in module that isn't used in the default theme namely the catalog/product_list_promotion rather than create something from scratch I thought I would try to modify this module and also use it as a base for the best seller module. So I found some examples of a best seller module and used the code to create best seller module, both the promotion module and the best seller module are working and displaying products. The problem is I can't apply category filters and I can't limit the number of results, I've tried every combination of adding the filters/limits/page sizes etc and either i get no errors but the original unchanged output or a fatal error to an undefined method. I have searched for hours for a solution and found some similar questions on SO and other forums, all suggest what I have already tried which hasn't worked, maybe someone can help?

So in Mage/Catalog/Product/Block/List/promotion.php I have:

class Mage_Catalog_Block_Product_List_Promotion extends Mage_Catalog_Block_Product_List
{
    protected function _getProductCollection()
    {
        if (is_null($this->_productCollection)) {
            $collection = Mage::getResourceModel('catalog/product_collection');
            Mage::getModel('catalog/layer')->prepareProductCollection($collection);
            // your custom filter
        $collection->getSelect()->order('rand()');
            $collection->addAttributeToFilter('promotion', 1)->addStoreFilter();
            $this->_productCollection = $collection;
        }
        return $this->_productCollection;
    }
}

I've tried adding $c...->setLimit(X), $c...->limit(X) $c...->getSelect()->limit(X), $c...->page(1, X), $c...->setPageSize(X) both before and after prepareCollection... In most cases I get no errors but no limit is applied. the best sellers module is essentially the same so wont post the code as I assume the issue with this will be the same for the best sellers module.

Any help would be greatly appreciated.

1

Tried adding $collection->load() and this seems to fix the problem, I assume my filters etc were being overridden in another class somewhere when the $collection is loaded. I didn't do this initially I was under the impression Magento didn't load the collection until it was needed allowing you to modify it at any point until it is loaded, I wanted to keep that functionality...

  • 1
    For any future visitors - if you call $collection->load explicitly you're effectively defeating lazy loading. – philwinkle Aug 13 '13 at 15:05

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.