How to list 4 random products from a categoy in Magento

How to list 4 random products from a categoy in Magento

August 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to list 4 random products from a categoy in Magento

How to List 4 Random Products from a Category in Magento

The question is pretty simple - the code below shows the items in one category. Those categories have 4 products.

However, my plan is to put in those categories 20 products, and make the following code to choose randomly 4 products of those categories.

I suppose the only line I need to change is this:

$_productCollection = $this->getLoadedProductCollection();

Randomly Selecting Products from a Category in Magento

To achieve this, we can use the Zend_Db_Select class along with the Mage_Catalog_Model_Resource_Product_Collection to create a random product selection query. Here’s an example of how you can modify your code:

$_productCollection = $this->getLoadedProductCollection();

// Create a new random order number for each product
$randomOrder = array();
foreach ($_productCollection as $_product) {
    $randomOrder[] = rand(0, 100); // Generate a random number between 0 and 100
}

// Sort the products based on the generated random numbers in descending order
array_multisort($randomOrder, SORT_DESC, $_productCollection);

// Limit the collection to the first 4 products
$_productCollection->setPageSize(4);

This code generates a random number for each product and sorts them in descending order. The setPageSize method is then used to limit the collection to the first 4 products.

Displaying Random Products in Magento

Now that you have your random product collection, you can modify the existing template structure to display these randomly selected products:

<?php if (!$_productCollection->count()): ?>
    <p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
    <div class="category-products">
        <?php $_iterator = 0; ?>

        <!-- Your existing template code here -->

        <?php foreach ($_productCollection as $_product): ?>
            <?php // Display random product here ?>
            <!-- Add your product display logic here using the $_product variable -->
        <?php endforeach; ?>
    </div>
<?php endif; ?>

In this modified structure, you need to replace the comment <!-- Add your product display logic here using the $_product variable --> with the code used in the existing template to display each product.

Conclusion

Randomly selecting 4 products from a category in Magento requires modifying the product collection and sorting it based on random numbers. By utilizing the Zend_Db_Select class along with the Mage_Catalog_Model_Resource_Product_Collection, you can achieve this functionality easily.

Remember to test your implementation thoroughly, ensuring that the random selection process works correctly and displays the desired products in your category page.

For further assistance with Magento development or any other web development needs, feel free to explore our services at Laravel Company.

Catalog Database Orders

Our network