0

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;
    }
}

1 Answer 1

0

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();

10
  • where should i include this piece of code in mine ? Commented Aug 13, 2014 at 10:44
  • thx for helping me, I posted the link to the page, and see the 5 images in the right part. I put the $this->_itemCollection->getSelect()->order('rand()'); before $this->_itemCollection->load(); BUT nothing happens. I refreshed the page multiple times but nothing. Any ideea why ? Commented Aug 13, 2014 at 10:52
  • ok, i wilk check
    – Amit Bera
    Commented Aug 13, 2014 at 10:53
  • thx a lot man :) Commented Aug 13, 2014 at 10:54
  • i have edit and it work in default magento
    – Amit Bera
    Commented Aug 13, 2014 at 11:05

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.