1

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

1

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.

6
  • how would i go about adding that into my code, sorry i am very new to magento and PHP. Thank you for your response though
    – Adam Allen
    May 21 '14 at 8:04
  • added more information and a link. May 21 '14 at 9:02
  • So just to see if i understand i have to add "catalog_block_product_list_collection" to my config.xml, then create a file called "Observer.php" and change "addInStockOnlyFilter" to "addCategoryFilter"and delete >joinField. Sorry for being such a noob at this.
    – Adam Allen
    May 21 '14 at 12:25
  • more or less, yes. You have to be sure, that the name of the method is the same in the class and in the config.xml and then you can modify the collection right before it is loaded in this observer May 21 '14 at 12:49
  • Hoepfully this will be my last question, but how do it edit {{block type="midmedssettings/product_list" num_products="5" category_id="145" template="catalog/product/random.phtml"}} to get the category id??
    – Adam Allen
    May 21 '14 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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