1

On the catalog product list view I can use shuffle() on the product collection however it only shuffles a small proportion (e.g. 10) of the full collection (hundreds). There is also infinite ajax scroll on this page. The shuffled products are those on the first 'page' before the scroll loads more.

$_productCollection=$this->getLoadedProductCollection();
....
$items = $_productCollection->getItems();
shuffle($items);
foreach ($items as $_product):
....

Is there any way I can keep the scrolling feature but shuffle the full collection?

One way to shuffle all products is the following, but this takes an age to load and has no scrolling.

$_productCollection = $this->getLoadedProductCollection();
$_productCollection->clear();
$_productCollection->setPage(1,99999);
1

I think you can better add a line like this:

$_productCollection->getSelect()->order(new Zend_Db_Expr('RAND()'));

This randomises the collection. but not does it external like shuffle does (just shuffling the array)

  • Thanks. I've tried this (in various places, like just before the foreach loop) but it has no effect, just shows the unrandomized collection. Is it compatible with getLoadedProductCollection? – Michael Mar 26 '15 at 12:09
  • I can get it to work with the following code instead of getLoadedProductCollection, but it just shows the full collection on the page, no page scrolling and takes long to load: $_productCollection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSort()->addAttributeToSelect('*')->addCategoryFilter(Mage::getModel('catalog/category')->load()); – Michael Mar 26 '15 at 12:17
  • Ok, so what I had to do was, as well as substituting out getLoadedProductCollection, was $_productCollection->setPageSize(20)->setCurPage(1); – Michael Mar 26 '15 at 12:35

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.