0

I have a Featured products category whihc has some of our best selling products in (15 products) I have them displaying on my home page. This section on my home page shows 5 random products at a time from my featured products category The problem i seem to be having is that it only shows 5 products on my home page and every time i refresh the page it shows the same 5 products but in a different order. Where as i want it to show 5 different products each time. i have a loop that goes through my products 5 times so i am not sure why it picking random products and just picking the same ones every time.

<div class="random_featured_products">
<?php
Mage::getSingleton('catalog/layer')->setData("product_collection",NULL);
$this->_productCollection = null;
$_productCollection=$this->getLoadedProductCollection();

?>
    <h4 class="recommended_title">
    <!--class="Recommended_title"-->
    <?php echo $this->__("Recommended For You");?></h4>
    <hr class="title_left">
<div class="products-grid two_columns_5">
<?php $_productCollection=$this->getLoadedProductCollection() ?>
<?php if(!$_productCollection->count()): ?>
<div class="padder">
<div class="note-msg">
    <?php echo $this->__('There are no products matching the selection.') ?>
</div>
</div>
<?php else: ?>

<?php // Grid Mode ?>
 <!-- the class name will change to .listing-type-cell if viewing in list mode -->
<?php $_collectionSize = $_productCollection->count() ?>

    <?php $_items = $_productCollection->getItems(); 
           shuffle($_items); ?>

    <table>
    <?php $i=0; foreach ($_items as $_product): ?>
    <?php if ($i++%4==0): ?>
    <tr>
    <?php endif ?>

    <ol class="grid">
        <li class="item1">
            <p class="product-image">
                <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getShortName()) ?>">
                    <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image'); ?>" alt="<?php echo $this->htmlEscape($_product->getShortName()) ?>"/>
                </a>
            </p>
            <p  class="product_desc" ><a  href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getShortName()) ?>"><?php echo $this->htmlEscape($_product->getShortName()) ?></a></p>
            <?php if($_product->getRatingSummary()): ?>
                <!--?php echo $this->getReviewsSummaryHtml($_product, 'short') ?-->
                <?php echo $this->getReviewsSummaryHtml($_product) ?>
            <?php endif; ?>
            <div class="pro_price">
            <?php echo $this->getPriceHtml($_product, true) ?>
            </div>

        </li>
    <?php if ($i%5==0 && $i!=$_collectionSize): ?>
    </tr>
    <?php endif ?>

    <?php if ($i==5) break;  // show 5 products max ?> 

    <?php endforeach ?>
    <?php for($i;$i%5!=0;$i++): ?>
          <td class="empty-product">&nbsp;</td>
    <?php endfor ?>
    <?php if ($i%5==0): ?>
    </tr>
    <?php endif ?>
    </table>
    <script type="text/javascript">decorateTable('product-list-table')</script>
</div>
</div></div>
<?php endif; ?>

If you can help thank you, if you cant thank you for at least reading my post

1

1 Answer 1

2

Instead of

<?php $_items = $_productCollection->getItems(); 
           shuffle($_items); ?>

try

<?php 
$_productCollection->getSelect()->order(new Zend_Db_Expr('RAND()'));
$_items = $_productCollection->getItems(); 
?>
4
  • nah now it is just showing the same 5 products in the same order.
    – Adam Allen
    Commented Jun 17, 2014 at 8:27
  • That's because the collection executes the sql query before we added the random order (i.e. when calling count() method). This means you should add the $_productCollection->getSelect()->order(new Zend_Db_Expr('RAND()')); line of code after $_productCollection=$this->getLoadedProductCollection(); (line 6 in your example)
    – Alex Dinca
    Commented Jun 17, 2014 at 8:33
  • I added $_productCollection->getSelect()->order(new Zend_Db_Expr('RAND()')); after $_productCollection=$this->getLoadedProductCollection(); and the other lines where you mentioned in the first answer and it is now back to displaying products in a random order but it is still only the same 5 products
    – Adam Allen
    Commented Jun 17, 2014 at 8:44
  • I love this approach - it's what Magento itself uses in EE for random associated product display.
    – philwinkle
    Commented Jul 16, 2014 at 20:32

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.