Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list 4 random products from a categoy in Magento

The question is pretty simple, the code below shows the items in one category. Those categories have 4 products. However my plan is to put in those categories 20 products, and make the following code to choose randomly 4 products of those categories.

I suppose the only line I need to change is this:

$_productCollection=$this->getLoadedProductCollection();



<?php
/**
 * Product list template
 *
 * @see Mage_Catalog_Block_Product_List
 */
?>
<?php
$_productCollection=$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 // List mode ?>

    <?php $_iterator = 0; ?>

    <script type="text/javascript">decorateList('products-list', 'none-recursive')</script>



    <?php // Grid Mode ?>

    <?php $_collectionSize = $_productCollection->count() ?>
    <?php $_columnCount = $this->getColumnCount(); $_columnCount=4; ?>
    <?php $i=0; foreach ($_productCollection as $_product): ?>
        <?php if ($i++%$_columnCount==0): ?>
        <ul class="products-grid">
        <?php endif ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <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(170); ?>" width="170" height="170" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
                 <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>
                <?php if($_product->getRatingSummary()): ?>
                <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                <?php endif; ?>

                <div class="actions">
                    <div style="clear:both;"></div>
                    <div>
                        <?php $Deal = $_product->getResource()->getAttribute('deal')->getFrontend()->getValue($_product);?>
                                <?php $onSale = $_product->getResource()->getAttribute('on_sale')->getFrontend()->getValue($_product);?>
                                <?php $hotItem = $_product->getResource()->getAttribute('hot_item')->getFrontend()->getValue($_product);?>
                                <?php $freeShip = $_product->getResource()->getAttribute('free_shipping')->getFrontend()->getValue($_product);?>
                                <?php if($Deal == 'Yes'){ ?>
                                    <img width="91px" height="21px" alt="Deal" title="Deal" src="<?php echo $this->getSkinUrl('images/icon-deal.gif') ?>" oncontextmenu="return false" >
                                <?php } ?>
                                <?php if($onSale == 'Yes'){ ?>
                                    <img width="91px" height="21px" alt="On Sale" title="On Sale" src="<?php echo $this->getSkinUrl('images/icon-sale.gif') ?>" oncontextmenu="return false" >
                                <?php } ?>
                                <?php if($hotItem == 'Yes'){ ?>
                                    <img width="91px" height="21px" alt="Hot Item" title="Hot Item" src="<?php echo $this->getSkinUrl('images/icon-hot.gif') ?>" oncontextmenu="return false" >
                                <?php } ?>
                                <?php if($freeShip == 'Yes'){ ?>
                                    <img alt="Free Shipping" width="91px" height="21px" title="Free Shipping" src="<?php echo $this->getSkinUrl('images/icon-freeship.gif') ?>" oncontextmenu="return false" >
                                <?php }?>
                    </div>
                </div>
                <?php echo $this->getPriceHtml($_product, true) ?>



                <div class="actions">
                    <?php if($_product->isSaleable()): ?>
                        <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>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                    <?php /*?><ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-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"><?php echo $this->__('Add to Compare') ?></a></li>
                        <?php endif; ?>
                    </ul><?php */?>
                </div>


            </li>
        <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
        </ul>
        <?php endif ?>
        <?php endforeach ?>
        <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>    


</div>
<?php endif; ?>
like image 643
Luis Valencia Avatar asked Dec 16 '22 05:12

Luis Valencia


1 Answers

Try this:

<?php

// $_productCollection = $this->getLoadedProductCollection();
 $_helper = $this->helper('catalog/output');
 $_category = Mage::getModel('catalog/category')->load($this->getCategoryId());
 $_productCollection = Mage::getResourceModel('reports/product_collection')
                       ->addAttributeToSelect('*')
                       ->addCategoryFilter($_category)
                       ->setVisibility(array(2,3,4));
 $_productCollection->getSelect()->order(new Zend_Db_Expr('RAND()'));                  
 $_productCollection->setPage(1, 4);

?>
like image 59
user487772 Avatar answered Dec 18 '22 18:12

user487772