0

How to get Random Product here? It always show first 5 product i want to show random product

<div class="Seller-other-product">

<?php $active = Mage::getStoreConfig('marketplace/admin_approval_seller_registration/displayproductpage');
    if ($active == 1) { 

    $sellerId      = Mage::registry('current_product')->getSellerId();

    $collection = Mage::getResourceModel('catalog/product_collection');
    $collection->addAttributeToFilter('status',1); //only enabled product
    $collection->addAttributeToSelect('*'); //add product attribute to be fetched
    $collection->addAttributeToFilter('seller_id',$sellerId);
    $collection->addStoreFilter();




    }

 ?>
<?php //echo $sellerId->getStoreTitle();?>
 <h1> Other Collection Of This Seller </h1>

<div class="seller-products">
                    <ul id = "seller-product-list1" class="sell-block">
                    <?php 
                        if (count($collection) > 0) {
                            $a = 1;
                                foreach($collection as $product){ ?>
                                <?php if($a%6==0) 
                                { 
                                    break;
                                }?>
                                <li class="item<?php if($a==1):endif;?>">
                                    <a href="<?php echo $product->getProductUrl(); ?>">
                                        <img class="product-image1" 
                                            <?php if($a==1):?>
                                                src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(200,280); ?>" 

                                            <?php else:?>
                                                src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(200,280); ?>" 
                                            <?php endif;?>
                                                alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" />
                                                <?php //echo $product->getName() ;?>


                                                <p class="product-name"><a title="<?php echo $this->escapeHtml($product->getName()) ?>" href="<?php echo $product->getProductUrl() ?>">  
                                    <?php 
                                    if (strlen($product->getName()) > 25) :
                                      echo strip_tags(substr($product->getName(), 0, 15)).'...';
                                    else : 
                                      echo $product->getName();
                                    endif;
                                  ?>
                                  </a>
                                                <?php echo $this->getPriceHtml($product); ?>
                                    </a>
                                </li>
                            <?php $a++;}
                    }?>
                </ul>

            </div>

</div>
4

2 Answers 2

1

I got Answer For this

I used rand() Function

$collection = Mage::getResourceModel('catalog/product_collection');
    $collection->addAttributeToFilter('status',1); //only enabled product
    $collection->addAttributeToSelect('*'); //add product attribute to be fetched
    $collection->getSelect()->order('rand()');//get random product
    $collection->addAttributeToFilter('seller_id',$sellerId);
    $collection->addStoreFilter();
0

You can add this to your collection to Randomize the results and limit to 1 in the results set

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

3
  • Although this would work it is worth noting that ordering a query randomly means the query can no longer be cached.
    – Smartie
    Commented Mar 21, 2016 at 20:15
  • And hence has a potential performance impact - Kothari didnt give any information as to the query size to judge so I plumped for the quickest/least work implementation Commented Mar 22, 2016 at 12:50
  • Sorry i am not getting
    – Magento 2
    Commented Mar 22, 2016 at 12:53

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.