Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

How to Display Random Product list on category/Product listing page in Magento

If you want to display or want to change your default product listing with random listing, just change following simple code.

The product listing page has already category filter applied, check the code at app/code/core/Mage/Catalog/Block/Product/List.php, you simply need to add the random filter there.

It is best practice to override your core file in your local directory instead of changing in core file. So copy file
from app/code/core/Mage/Catalog/Block/Product/List.php to app/code/local/Mage/Catalog/Block/Product/List.php

Now open this file Search _getProductCollection() function. In this function find out
$this->_productCollection = $layer->getProductCollection(); and comment this line and add following code just after this line...

$collection = $layer->getProductCollection();
$collection->getSelect()->order('rand()');
$this->_productCollection = $collection;

Now clear your cache and you are done.

Please support us, Like us on Facebook.

  1. This solution is working great but there is one problem the products was getting repeated in another page can you give me any solution for that.....i am waiting for your reply

    ReplyDelete
    Replies
    1. Which magento version you are using? can please clear your magento cache one and check again.

      Delete
  2. Thank you, I tried many days to get let the random products in the categories

    ReplyDelete
  3. Hello Hardik,
    I am Rahul Modi we have to talk with your more then time in last some times. i have issue related that i use this in my one site but no changes are there my magento version is 1.7.

    ReplyDelete

 

Copyright @ 2017 HKBlog.