Limit number of products displayed in random products block in Magento

Limit number of products displayed in random products block in Magento

July 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Limit number of products displayed in random products block in Magento

Limiting Products Displayed in Random Product Blocks on Magento Homepage

Introduction

When customizing your Magento store’s homepage, you might want to display a random selection of products using the catalog/product_list_random block type. However, by default, the number of products shown is controlled by the “Products per Page on Grid Default Value” setting in the backend configuration. This can lead to inconsistent results with your custom layout updates.

In this post, we’ll explore how to apply the solution from StackOverflow’s popular question Magento limit the number of products shown with in the new products block to a random product block on your homepage.

The Current Problem

You’re currently using this layout XML code to set the number of random products:

<reference name="random">
    <block type="catalog/product_list_random" name="product_random" template="catalog/product/grid_only.phtml">
        <action method="setProductsCount"><count>3</count></action>
    </block>
</reference>

However, you’re finding that the setProductsCount action isn’t being honored. The product count on your homepage is still dominated by the “Products per Page on Grid Default Value” setting from the System > Configuration > Catalog > Catalog front-end section in your Magento backend.

The Solution: Overriding the Block Class

To resolve this issue, we need to override the Mage_Catalog_Block_Product_List_Random class and ensure that our custom setProductsCount method takes precedence over the default configuration setting. Here’s a step-by-step guide:

  1. Create Your Custom Module

Ensure you have a functional Magento module set up with appropriate etc/modules.xml, app/etc/modules/Your_Company_Your_Module.xml, and relevant directory structure.

  1. Override the Block Class

In your module’s Block folder, create a new file: Product/List/Random.php. This will contain your custom block class:

class Your_Company_Your_Module_Block_Product_List_Random extends Mage_Catalog_Block_Product_List_Random {
    public function setProductsCount($count) {
        $this->setProductCollection(
            $this->getProductCollection()->addAttributeToSelect('*')
                ->setPageSize($count)
        );
    }
}

In this file, we’re extending the original Mage_Catalog_Block_Product_List_Random class and overriding its setProductsCount method. We’re ensuring that our custom count takes precedence over any system configuration settings.

  1. Configure Your Layout XML

Update your layout XML in the appropriate location (usually under layout/cms.xml for homepages with CMS content) to use your new block type:

<reference name="random">
    <block type="yourcompany_yourmodule/catalog_product_list_random" name="product_random" template="catalog/product/grid_only.phtml">
        <action method="setProductsCount"><count>3</count></action>
    </block>
</reference>

Replace yourcompany_yourmodule with the actual namespace of your custom module.

Conclusion

By following these steps, you’ve successfully overridden the default Magento behavior and ensured that your custom random product count is used on the homepage. This approach provides better control over the layout of your store’s key pages while still allowing for flexibility in configuration elsewhere.

Remember to clear your cache after updating your layout XML files. You can do this manually or use a script like the one available at Laravel Company’s Magento Cache Management Guide.

This solution demonstrates how careful class overriding and proper module structure can help you achieve custom functionality within Magento’s flexible architecture. If you have any other questions about layout customization or need assistance with a specific Magento development project, don’t hesitate to reach out to the expert team at Laravel Company.

Catalog Themes Admin Configuration

Our network