2

In catalog/product/list.phtml, how can I always use the products from a single, saved product collection?

Is there a way to load a product collection, save it, then show some of these products (e.g. 20) per page without having to reload the product collection again when a new page is reached (either clicked or in infinite scrolling)?

I considered a couple of ways, but my understanding falls way short.

  1. Create an observer which filters out any products that have already appeared.

    //catalog_product_collection_load_before
    
    $productCollection = $observer->getEvent()->getCollection();
    $currentPage = (int) Mage::App()->getRequest()->getParam('p');
    $seen = array();
        foreach($productCollection as $item){ // getting 503 on page 2
            $seen[] = $item->getId();
        }
        $productCollection->clear();
        $productCollection->addAttributeToFilter('entity_id', array('nin' => $seen));
        $productCollection->setCurPage($currentPage)->setPageSize(20);
    
  2. Some kind of controller (indexAction)

    $productsCollection->setCurPage($currentPage);
    $productsCollection->load();
    
    foreach ($productsCollection as $_product) {
    
        // ???
    }
    
    $currentPage++;
    
    $productsCollection->clear();
    

Of course these are just fragments. Any pointers would be much appreciated.

2 Answers 2

1

The quickest way I see, is to show the full collection in the page and just hide all the products (display:none) after the first 20.
Then, when clicking on the next page, just show 20 more items.
But I don't think that what you want is a good idea.
The paging exists for a reason. Not to load unneeded resourced from the db.
You may get into performance issues.

1
  • Thanks, that's one way. Performance is already an issue so paging is the way forward but I have to somehow use the same collection on page 2, 3, 4 as was created for page 1.
    – MW Millar
    Jun 11, 2015 at 13:38
1

You do not order them by random, therefore the order is implicit. You can just use the second page? Why do you think, the products are shown twice?

If you want to make sure, this doesn't happen, then just order the products by something which doesn't change, like sku or id.

you can even order by RAND() but give rand a static seed, like the current date or month?

Note on your code:

foreach($productCollection as $item){ // getting 503 on page 2
    $seen[] = $item->getId();
}

foreach loads a collection, no clue why 503, but afterwards you can't filter any more.

to debug: Fundamentals for debugging a Magento store

1
  • That's helpful thanks. I had nothing showing in the logs, despite all logging on. Actually I have been testing out a random collection. I'm trying to do it in small steps, so envisaged this question to be the first step, before I tackled the randomisation. On each subsequent page the collection is reloaded, so products appear that are already on the page.
    – MW Millar
    Jun 11, 2015 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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