4

On my cart page i have a little display, which shows products from a certain category, it displays 4 products, but in my category i have 8-10 products. How can i make it select random products so it changes every time the page is refreshed.

This is my code for the products at the moment

<?php if (Mage::helper('checkout/cart')->getQuote()->getSubtotal() < 75.00)
{?>
 <h1><?php echo 'We see your subtotal is under £75, why not add these products to qualify for Free Delivery';?></h1>
 <br>
<?php

$categoryid = 1060;

$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
?>
<div class="upsell_products">

<?php foreach ($collection as $_product) { ?>


<div class="product">
<a href="<?php echo $_product->getProductUrl() ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image'); ?>" alt="" /></a>
<a href="<?php echo $_product->getProductUrl(); ?>">
<p class="upsell_pro_name"><?php echo $_product->getName(); ?></p>
<p class="upsell_pro_price"><?php echo Mage::helper('core')->formatPrice($_product->getPrice());?></a></p>

</div>
<?php } }

If you can help thank you :)

8

Try using:

$categoryid = 1060;
$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSort()
    ->addAttributeToSelect('*')
    ->addCategoryFilter(Mage::getModel('catalog/category')->load($categoryid));
$products->getSelect()->order(new Zend_Db_Expr('RAND()'));

Source here.

Probably putting this after your load code might work too:

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

There are two issues with the usual approach:

  1. ORDER BY RAND() is unperformant because it results in a resource intensive temp table copy. It has to load all results into a temporary table, assign a random number to each row and then sort without any index. You can read about it in detail in my blog: http://www.schmengler-se.de/en/2015/09/show-random-products-in-magento-you-are-doing-it-wrong/

  2. addAttributeToSelect('*') loads all product attributes, which is unperformant, given Magento's EAV structure.

Performant Solution:

Replace

$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');

With

$collection = $category->getProductCollection();

$numberOfItems = 4;
$candidateIds = $collection->getAllIds();
$choosenIds = [];
$maxKey = count($candidateIds)-1;
while (count($choosenIds) < $numberOfItems)) {
    $randomKey = mt_rand(0, $maxKey);
    $choosenIds[$randomKey] = $candidateIds[$randomKey];
}    
$collection->addIdFilter($choosenIds);

$collection
    ->addMinimalPrice()
    ->addFinalPrice()
    ->addTaxPercents()
    ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
    ->addUrlRewrite();

This will retrieve all ids (this is fast and the amount of data is managable even for large catalogs), pick some randomly and retrieve these rows directly.

Also we specify to load only the attributes marked as "used in product listing" and join price and URL indexes to be able to show the right price and product URL.

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.