is it possible in Magento 2 to randomly show the products on a category page, like everytime the page is opened, random products are shown?



1 Answer 1


To achieve this you have to override the catelog_category_view.xml

And Change Block of getting listing of product collection.

<block class="Magento\Catalog\Block\Product\ListProduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">

Change class from Magento\Catalog\Block\Product\ListProduct to Magento\Catalog\Block\Product\ProductList\Random like below i have updated.

<block class="Magento\Catalog\Block\Product\ProductList\Random" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">

After that make preference for _getProductCollection() for getting current category product like below.

     * @return \Magento\Catalog\Model\ResourceModel\Product\Collection
    protected function _getProductCollection()
        if ($this->_productCollection === null) {
            /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
            $collection = $this->_productCollectionFactory->create();
            $collection->addCategoriesFilter(['in' => <<Category Id for getting current category product>>]);
            $numProducts = $this->getNumProducts() ? $this->getNumProducts() : 0;
            $collection->setPage(1, $numProducts);

            $this->_productCollection = $collection;
        return $this->_productCollection;

After that clear cache and check you getting product collection randomly. Still if you have any query let me know.

Your Answer

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

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