0

i want to Display random products on product page of magento every time i refresh the page .

sorry in advance i have googled a lot nothing seems to be working or without any instructions

list.phtml code

<?php
/**
 * Product list template
 *
 * @see Mage_Catalog_Block_Product_List
 */
?>
<?php
    $_productCollection=shuffle($this->getLoadedProductCollection());
    $_helper = $this->helper('catalog/output');
?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
    <?php echo $this->getToolbarHtml() ?>
    <?php // List mode ?>
    <?php if($this->getMode()!='grid'): ?>
    <?php $_iterator = 0; ?>
    <ol class="products-list" id="products-list">
    <?php foreach ($_productCollection as $_product): ?>
    <?php 
    $Psku = $_product->getSku();
    $_Pdetails = Mage::getModel('catalog/product')->loadByAttribute('sku',$Psku);
    if($_Pdetails){
        $productId = $_Pdetails->getId();


        $resource = Mage::getSingleton('core/resource');
        $readConnection = $resource->getConnection('core_read');
        $query = "SELECT userid FROM marketplace_product where mageproductid = '$productId'";
        $results = $readConnection->fetchAll($query);
        $artist_id = $results[0]['userid'];

        $artist_query = "SELECT logopic FROM marketplace_userdata where mageuserid = '$artist_id'";
        $artist_results = $readConnection->fetchAll($artist_query);
        $artist_pic = $artist_results[0]['logopic'];
        if(isset($artist_pic) && $artist_pic!=''){

        }else{
            $artist_pic = 'np_thumb2.gif';
        }
        $mediaUrl =  Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
        $artist_pic = $mediaUrl."avatar/".$artist_pic;
        $customer_data=Mage::getModel('customer/customer')->load($artist_id)->getData();

        $artist_name = $customer_data['firstname']." ".$customer_data['lastname'];
        $productUrl = $_Pdetails->getProductUrl();
        $productName = $_Pdetails->getName();
        $productPrice = $_Pdetails->getPrice();
    }   
    ?>
    <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
            <?php // Product Image ?>
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(300,300); ?>"  alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
            <?php // Product description ?>
            <div class="product-shop">
                <div class="f-fix">
                    <?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
                    <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a></h2>
                    <div>
                      <p class="artist-nam-grid"><?php echo $artist_name;?></p>
                      <p class="artist_img-grid"><img src="<?php echo $artist_pic;?>" alt="<?php echo $artist_name;?>" title="<?php echo $artist_name;?>" height="50" width="50"/></a>
                    </div>
                    <?php if($_product->getRatingSummary()): ?>
                    <?php echo $this->getReviewsSummaryHtml($_product) ?>
                    <?php endif; ?>
                     <?php echo $this->getPriceHtml($_product, true) ?>
                    <div class="desc std">
                        <?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                        <!-- <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped ?>" class="link-learn"><?php echo $this->__('Learn More') ?></a> -->
                    </div>      
                    <?php if($_product->isSaleable()): ?>
                        <p class="product-list-button"><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
                    <?php else: ?>
                        <p class="product-list-button availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                    <div class="product-detail"><a class="product-detail-link" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->__('Details') ?>" rel="tooltip"><?php echo $this->__('Details') ?></a></div>
                    <!--<ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist" rel="tooltip" title="<?php echo $this->__('Add to Wishlist') ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                            <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare" rel="tooltip" title="<?php echo $this->__('Add to Compare') ?>"><?php echo $this->__('Add to Compare') ?></a></li>
                        <?php endif; ?>
                    </ul>-->
                </div>
            </div>
        </li>
    <?php endforeach; ?>
    </ol>
    <script type="text/javascript">decorateList('products-list', 'none-recursive')</script>

    <?php else: ?>

    <?php // Grid Mode ?>
    <?php
        $_span = 'span4';
        $_grid_pro = Mage::getStoreConfig('themeoptions/themeoptions_config/grid_product');
        if ( !$_grid_pro ) { $_grid_pro = 3; }
        switch ($_grid_pro)
        {
        case 2:
            $_span = 'span6';
            break;
        case 3:
            $_span = 'span4';
            break;
        case 4:
            $_span = 'span3';
            break;
        }
    ?>

    <?php $_collectionSize = $_productCollection->count() ?>
    <?php $_columnCount = $_grid_pro; //$this->getColumnCount(); ?>
    <?php $i=0; foreach ($_productCollection as $_product): ?>

    <?php 
    $productprice = $_product->getPrice();
    $Psku = $_product->getSku();
    $_Pdetails = Mage::getModel('catalog/product')->loadByAttribute('sku',$Psku);
    if($_Pdetails){
        $productId = $_Pdetails->getId();


        $resource = Mage::getSingleton('core/resource');
        $readConnection = $resource->getConnection('core_read');
        $query = "SELECT userid FROM marketplace_product where mageproductid = '$productId'";
        $results = $readConnection->fetchAll($query);
        $artist_id = $results[0]['userid'];

        $artist_query = "SELECT logopic FROM marketplace_userdata where mageuserid = '$artist_id'";
        $artist_results = $readConnection->fetchAll($artist_query);
        $artist_pic = $artist_results[0]['logopic'];
        if(isset($artist_pic) && $artist_pic!=''){

        }else{
            $artist_pic = 'np_thumb2.gif';
        }
        $mediaUrl =  Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
        $artist_pic = $mediaUrl."avatar/".$artist_pic;
        $customer_data=Mage::getModel('customer/customer')->load($artist_id)->getData();

        $artist_name = $customer_data['firstname']." ".$customer_data['lastname'];
        $productUrl = $_Pdetails->getProductUrl();
        $productName = $_Pdetails->getName();
        $productPrice = $_Pdetails->getPrice();
        $artistDATA=Mage::getModel('marketplace/userprofile')->getPartnerProfileById($artist_id);
        if($artistDATA['wantpartner']==1){
            $profileUrl = $artistDATA['profileurl'];
        }else{
            $profileUrl = '';
        }

    }   
    ?>




        <?php if ($i++%$_columnCount==0): ?>
        <div class="products-grid row-fluid">
        <?php endif ?>
            <div class="<?php echo $_span ?> item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <!-- <div class="item-inner"> remove box shadow-->
                <div class="">

                <?php 
                    $height = $this->helper('catalog/image')->init($_product, 'small_image')->getOriginalHeight();
                    $width = $this->helper('catalog/image')->init($_product, 'small_image')->getOriginalWidth();

                    if($height<$width){
                        $heightOrignal = $height;
                        $heightRe = 226;
                        $ratio = $heightOrignal/226;
                        $widthRe = $width/$ratio;

                        if($widthRe>=226){

                        }else{
                            $widthRe = 226;
                        }
                    }else{
                        $widthOrignal = $width;
                        $widthRe = 226;
                        $ratio = $widthOrignal/226;
                        $heightRe = $height/$ratio;
                        if($heightRe>=226){

                        }else{
                            $heightRe = 226;
                        } 
                    }
                    $imageHeightDiff = (226 - $heightRe)/2;
                    $imageWidthDiff = (226 - $widthRe)/2;
                ?>
                    <a href="<?php echo $_product->getProductUrl() ?>" title="" class="product-image-listing"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image'); ?>"  alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>"  style="width:<?php echo $widthRe."px";?>;max-width:<?php echo $widthRe."px";?>;margin-top:<?php echo $imageHeightDiff."px";?>; margin-left:<?php echo $imageWidthDiff."px";?>;"/></a>
                    <div class="product-info">  
                            <h2 class="product-name pro_name"><a href="<?php echo $_product->getProductUrl() ?>" title=""><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a>
                            <div style="clear:both"></div>
                             <a class="artist_name_a" href="<?php echo  Mage::getUrl()."marketplace/seller/profile/".$profileUrl."/list"; ?>">
                                        <?php echo $artist_name;?>
                                    </a></h2>

                             <?php //if($_product->isSaleable()): ?>
                                <?php if($productPrice != 0){?>
                                    <?php echo $this->getPriceHtml($_product, true) ?>
                                <?php } ?>
                            <?php //endif; ?>
                            <?php if($_product->getRatingSummary()): ?>
                            <?php //echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                            <?php endif; ?>
                            <!-- <div class="artist_div">
                                <p class="artist-name">
                                    <a href="<?php echo  Mage::getUrl()."marketplace/seller/profile/".$profileUrl."/list"; ?>">
                                        <?php echo $artist_name;?>
                                    </a>
                                </p>
                                <p class="artist_img">
                                    <a href="<?php echo  Mage::getUrl()."marketplace/seller/profile/".$profileUrl."/list"; ?>">
                                        <img src="<?php echo $artist_pic;?>" alt="<?php echo $artist_name;?>" title="<?php echo $artist_name;?>" height="50" width="50"/>
                                    </a>
                                </p>

                            </div> -->
                            <div class="action_div"> 

                            <div class="actions" style="width: 90%; margin: 0 0 10px 0;">
                                <?php if(!$_product->isSaleable()): ?>
                                    <?php if($productprice != 0){?>
                                    <button type="button" class="button btn-cart" style="float: left;cursor: inherit;" ><span><span><?php echo $this->__('Sold') ?></span></span></button>
                                    <?php }else{?>
                                    <button type="button" class="button btn-cart" style="float: left;cursor: inherit;" ><span><span><?php echo $this->__('On Display') ?></span></span></button>
                                    <?php } ?>
                                <?php endif; ?>

                                <?php if($_product->isSaleable()): ?>
                                <?php if($productprice != 0){?>
                                    <button type="button" title="<?php echo $this->__('') ?>" class="button btn-cart" style="float: left;cursor: pointer;" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Buy now') ?></span></span></button>
                                 <?php }else{?>
                                    <button type="button" class="button btn-cart" style="float: left;cursor: inherit;" ><span><span><?php echo $this->__('On Display') ?></span></span></button>
                                <?php  } ?>  
                                <?php endif; ?>

                                <ul class="add-to-links" style="float: right; margin: 0;">
                                    <?php if ($this->helper('wishlist')->isAllow()) : ?>
                                        <li>
                                        <a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"  rel="tooltip">
                                            <button type="button" title="<?php echo $this->__('') ?>" class="button btn-cart"><span><span><?php echo $this->__('Add to Wishlist') ?></span></span></button>
                                        </a>    

                                        <!--  <a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist" title="<?php echo $this->__('Add to Wishlist') ?>" rel="tooltip"><?php echo $this->__('Add to Wishlist') ?></a> -->
                                        </li>
                                       <?php endif; ?>
                                    <?php /* if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                                        <li><a href="<?php echo $_compareUrl ?>" class="link-compare" title="<?php echo $this->__('Add to Compare') ?>" rel="tooltip"><?php echo $this->__('Add to Compare') ?></a></li>
                                    <?php endif; */?>
                                </ul>

                            </div>
                            </div>
                    </div>
                                <?php
                                    $newFromDate = Mage::getModel('catalog/product')->load($_product->getID())->getNewsFromDate();
                                    $newToDate = Mage::getModel('catalog/product')->load($_product->getID())->getNewsToDate();                 
                                    $now = date("Y-m-d H:m:s");

                                    if($newFromDate <= $now && $newToDate >= $now && Mage::getStoreConfig('themeoptions/themeoptions_config/new_label')) {
                                ?>
                                        <div class="label-pro-new"></div>
                                <?php } ?>

                                <?php 
                                    // Get the Special Price
                                    $specialprice = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialPrice(); 
                                    // Get the Special Price FROM date
                                    $specialPriceFromDate = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialFromDate();
                                    // Get the Special Price TO date
                                    $specialPriceToDate = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialToDate();
                                    // Get Current date

                                    if ($specialprice){
                                        if($specialPriceFromDate <= $now && $specialPriceToDate >= $now && Mage::getStoreConfig('themeoptions/themeoptions_config/sale_label')) {
                                ?>
                                                <div class="label-pro-sale"></div>
                                <?php
                                    } }
                                ?>             
                </div>
            </div>
        <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
        </div>
        <?php endif ?>
        <?php endforeach ?>
        <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
    <?php endif; ?>

    <div class="toolbar-bottom">
        <?php echo $this->getToolbarHtml() ?>
    </div>
</div>
<?php endif; ?>
0

2 Answers 2

1

Try this:

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

And then your foreach

2
-1

I can suggest simple way to do this

in your list.phtml file find at first line

$_productCollection=$this->getLoadedProductCollection();

and replace with

$_productCollection=shuffle($this->getLoadedProductCollection());

4
  • m getting black input for after it .. m uploading my list.phtml in my question for you to see .. and pls suggest a edit Commented Mar 13, 2014 at 13:43
  • I have update the code check and let me know
    – Keyul Shah
    Commented Mar 13, 2014 at 14:02
  • m getting this error Fatal error: Call to a member function count() on a non-object in /opt/lampp/htdocs/artonair/app/design/frontend/default/ma_deluxe/template/catalog/product/list.phtml on line 38 Commented Mar 14, 2014 at 4:27
  • Even if shuffle() would work on collections (it doesn't), this would only show the same products in a different order. Commented Feb 17, 2015 at 9:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.