This is the piece of code that generates the cross products:

<?php $_helper = $this->helper('catalog/output'); ?>
    $_product = $this->getProduct(); 
   // echo get_class($this); 

<?php if($_crossSellProducts = $_product->getCrossSellProducts()): ?>

    <div class="crosssell">
        <h2><?php echo $this->__('Suntem mandri sa va prezentam clientii TRENDfurniture:') ?></h2>

        <ul id="crosssell-products-list">
        <?php foreach ($_crossSellProducts as $_item): ?>

            $_item = Mage::getModel('catalog/product')->load($_item->getId()); 
            <li class="item">
                <a class="product-image" href="<?php echo $_item->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>">
                    <img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail'); ?>" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" />

        <?php endforeach; ?>

        <script type="text/javascript">decorateList('crosssell-products-list', 'none-recursive')</script>
<?php endif;?>

I tried to use:

 $_item ->getSelect()->order(new Zend_Db_Expr('RAND()'));                  
 $_item ->setPage(1, 4);

but with no result.This is the result of the get_class function : Mage_Catalog_Block_Product_View

  • 1
    Side note - loading products one by one inside the loop is not a good practice.
    – Amasty
    Commented Aug 13, 2014 at 13:54
  • @Amasty I inherited it, I just want to display it randomly and put a limit. is this possible ? Commented Aug 13, 2014 at 13:56

3 Answers 3


create a function on view.php

    public function getCrosspro(){
        $product = $this->getProduct();
        /* @var $product Mage_Catalog_Model_Product */

        $itemCollection = $product->getCrossSellProductCollection()

            $itemCollection->getSelect()->order(new Zend_Db_Expr('RAND()')); 
            $itemCollection->setPage(1, 4);

        foreach ($itemCollection as $product) {

return $itemCollection

and change:

<?php if($_crossSellProducts = $_product->getCrossSellProducts()): ?>


<?php if($_crossSellProducts = $this->getCrosspro()): ?>
  • woaw, you saved my day, but can you give me more explanation to understand what does function exacty do ? thx a lot Commented Aug 13, 2014 at 14:07
  • Ya I will reply shortly
    – Amit Bera
    Commented Aug 13, 2014 at 14:12

To implement custom logic for cross sell products, you can override the method getCrossSellProducts or call a method from your own class where do all the magic.

If you need just use the default logic, but sort products randomly each time you need to add sorting by rand before the collection is loaded, not after, as it has no effect.

Edit: Based on your code, apply sorting and limitation on $_crossSellProducts before the loop.

  • You could also override getCrossSellProductCollection(), which is called by getCrossSellProducts() - just another option. Commented Aug 13, 2014 at 14:05
  • Your suggestion to apply sorting and limitation on $_crossSellProducts won't work, because getCrossSellProducts() returns an array of products, not an actual collection. Commented Aug 13, 2014 at 14:07
  • @Colin Thanks for the correction, not at the pc now and my memory servers me bad (
    – Amasty
    Commented Aug 13, 2014 at 14:13

The cross-sell product collection can be obtained using this code:

$_crossSellProductCollection = $_product->getCrossSellProductCollection();

You'd then use that to set the random order and page size:

$_crossSellProductCollection->getSelect()->order(new Zend_Db_Expr('RAND()'));
$_crossSellProductCollection->setPage(1, 4);

Edit: You can then iterate over those products like so:

<?php foreach ($_crossSellProductCollection as $_item): ?>
<?php endforeach; ?>
  • and this should i include where ? Commented Aug 13, 2014 at 14:02
  • im getting this error: Fatal error: Call to a member function getSelect() on a non-object ..... Commented Aug 13, 2014 at 14:03
  • You can include this in your template file. Commented Aug 13, 2014 at 14:04

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.