Generate Random Related Products in Magento – How to

random

I have this piece of code, which generates me 6 related products from a total number of products. I want to generate it randomly. Is there a function in magento to do that ? thx

This is the LINK to the page

<?php
    $_collectionSize = count($this->getItemCollection()->getItems());
    $x = $this->getItemCollection()->getItems();
    //var_dump($x);
?>
<?php if($_collectionSize): ?>
<?php
    $theme = $this->helper('ultimo');


    //Default image size
    $imgWidth = 220;
    $imgHeight = 220;

    if ($theme->getCfg('category/aspect_ratio'))
        $imgHeight = 0; //Height will be computed automatically (based on width) to keep the aspect ratio

    //Modify slider depending on number of slides
    $sliderClasses = '';
    if($_collectionSize > 0)
    {
        if($_collectionSize <= 5)
            $sliderClasses .= " count-$_collectionSize";
        else
            $sliderClasses .= " count-multi";
    }
?>

<h3 class="section-title"><?php echo $this->__('Suntem mandri sa va prezentam clientii TRENDfurniture:'); ?></h3>
<div class="upsell-itemslider-wrapper itemslider-wrapper">

    <div class="nav-wrapper gen-slider-arrows1 gen-slider-arrows1-pos-top-right">
    </div>

    <div class="itemslider itemslider-horizontal itemslider-x<?php if($sliderClasses) echo $sliderClasses; ?>">
        <ul class="slides products-grid">
            <?php $this->resetItemsIterator() ?>
            <?php foreach($this->getItemCollection()->getItems() as $_product): //$_link replaced with $_product ?>
                <li class="item">
                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="product-image"><img src="<?php echo $theme->getImgUrl($this, $_product, $imgWidth, $imgHeight, 'thumbnail'); ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" /></a>
                </li>
            <?php endforeach; ?>
        </ul> <!-- end: slides -->
    </div> <!-- end: itemslider -->

</div> <!-- end: itemslider-wrapper -->
<?php endif; ?>

This is hte Related.php content:

class Mage_Catalog_Block_Product_List_Related extends Mage_Catalog_Block_Product_Abstract
{
    /**
     * Default MAP renderer type
     *
     * @var string
     */
    protected $_mapRenderer = 'msrp_noform';

    protected $_itemCollection;

    protected function _prepareData()
    {
        $product = Mage::registry('product');
        /* @var $product Mage_Catalog_Model_Product */

        $this->_itemCollection = $product->getRelatedProductCollection()
            ->addAttributeToSelect('required_options')
            ->setPositionOrder()
            ->addStoreFilter()
        ;

        if (Mage::helper('catalog')->isModuleEnabled('Mage_Checkout')) {
            Mage::getResourceSingleton('checkout/cart')->addExcludeProductFilter($this->_itemCollection,
                Mage::getSingleton('checkout/session')->getQuoteId()
            );
            $this->_addProductAttributesAndPrices($this->_itemCollection);
        }
//        Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($this->_itemCollection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_itemCollection);
        $this->_itemCollection->getSelect()->order('rand()');
        $this->_itemCollection->load();

        foreach ($this->_itemCollection as $product) {
            $product->setDoNotUseCategoryId(true);
        }

        return $this;
    }

    protected function _beforeToHtml()
    {
        $this->_prepareData();
        return parent::_beforeToHtml();
    }

    public function getItems()
    {
        return $this->_itemCollection;
    }
}

Best Answer

Chester

Go to block class Mage_Catalog_Block_Product_List_Related and goto function _prepareData()

and add this code

$this->_itemCollection->getSelect()->order('rand()');
    $this->_itemCollection->setPage(1, 5);// 5 no of product you want to show

Before: $this->_itemCollection->load();

Related Topic