Magento Category – How to Show Random Products on Category Landing Pages

category

I want to show 4 random products on the each category landing page, I had a way of doing this using

{{block type="midmedssettings/product_list" num_products="5" category_id="145" template="catalog/product/random.phtml"}}

and the .phtml file look like this

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

<?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 ?>
<div class="listing-type-grid  catalog-listing padder"> <!-- 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 cellspacing="0" class="generic-product-grid" id="product-list-table">
    <?php $i=0; foreach ($_items as $_product): ?>
    <?php if ($i++%4==0): ?>
    <tr>
    <?php endif ?>
        <td>
            <p class="product-image">
                <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
                    <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(170, 170); ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>"/>
                </a>
            </p>
            <h5><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h5>
            <?php if($_product->getRatingSummary()): ?>
                <!--?php echo $this->getReviewsSummaryHtml($_product, 'short') ?-->
                <?php echo $this->getReviewsSummaryHtml($_product) ?>
            <?php endif; ?>

            <?php echo $this->getPriceHtml($_product, true) ?>

            <div class="clear"></div>
            <ul class="add-to">

                <?php if($_product->isSaleable()): ?>
                <li class="add-to-cart-textlink"><a href="#" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span>Add to Cart</span></a></li>
                <?php else: ?>
                <li><?php echo $this->__('Out of stock') ?></li>
                <?php endif; ?>

                <?php if ($this->helper('wishlist')->isAllow()) : ?>
                <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
                <?php endif; ?>
                <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                <li><a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a></li>
                <?php endif; ?>
            </ul>
        </td>
    <?php if ($i%4==0 && $i!=$_collectionSize): ?>
    </tr>
    <?php endif ?>

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

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

<?php endif; ?>

However if i make a static block with this in i will have to make 37 different blocks for each category as it finds the category form the category id, is there a way i can get it to pull the category id so i do not have to specify it and one static block can be used in any category??

If this isnt clear please just write a comment and not vote down as i really need to sort this.

Thank you in advance

Best Answer

you can get the used category from:

$category = Mage::registry('category') or $category = Mage::registry('current_category')

To do so, you want to implement an observer listening on catalog_block_product_list_collection and instead of joining and filtering for stock options, you add a category filter: addCategoryFilter based on the registry entry.

Related Topic