0

How can I limit this block of code to display only 4 images from the category?

   <?$_productCollection=$this->getLoadedProductCollection()?>
   <?if(!$_productCollection->getSize()):?>
   <div class="note-msg">
   <?=$this->__('There are no products matching the selection.')?>
   </div>
   <?else:?>

   <div class="home-spot">
   <div class="box best-selling">
   <h3><?=$this->__('Accesssories')?></h3>
   <table cellspacing="0">
   <?$i=0;$j=0; foreach ($_productCollection as $_product): ?>
   <?if ($i%2==0):?>
   <tr class="<?=($j++%2==0)?'even':'odd'?>">
   <?endif?>
   <td>                                           
   <a href="<?=$_product->getProductUrl()?>"><img src="<?=$this->helper('catalog/image')->init($_product, 'small_image')->resize(95, 95);?>" width="95" height="95" alt="<?=$this->htmlEscape($_product->getName())?>"/></a>
   <div class="product-description">
   <p><a href="<?=$_product->getProductUrl()?>" class="product-name"><?=$this->htmlEscape($_product->getName())?></a></p>
   <a href="<?php echo $this->getAddToCartUrl($_product) ?>" data-url="<?php echo $this->getAddToCartUrl($_product) ?>" data-id="<?php echo $_product->getId(); ?>" title="<?php echo $this->__('Add to Cart') ?>" class="btn btn-primary ajax-cart" ><i class="icon-cart"></i> <?php echo $this->__('Add to Cart') ?></a>
   </div>
   </td>
   <?if ($i%2!=0):?>
   </tr>
   <?endif?>
   <?$i++?>
   <?php endforeach?>
   </table>
   </div>
   </div>
   <?endif;?>

I'm displaying the results like this here in my category

{{block type="catalog/product_list" category_id="22" products_count="4" columnCount="4" template="catalog/category/random.phtml"}}

however products_count="4" doesn't work. it just displays all images in that category. Is there some php I can used to control,

<?$_productCollection=$this->getLoadedProductCollection()?>
0

I think I figured it out but I'm not sure if it's the cleanest method. please advise as it works fine now with limitation. what I did was just add these lines of code to the top.

<?$_productCollection=$this->getLoadedProductCollection();
$_productCollection->clear();
$_productCollection->setPageSize(3);
$_productCollection->load(); ?>
<?if(!$_productCollection->getSize()):?>

Now I can limit the number of products by adding a number to setPageSize(); However I'm not sure if I should be doing this or not. But it works great :) woo hoo!!!

0

Try:

{{block type="catalog/product_list" category_id="22" product_limit="4" columnCount="4" template="catalog/category/random.phtml"}}

Or:

$_productCollection=$this->getLoadedProductCollection();
_productCollection->clear()
                    ->setPageSize(4)
                    ->load();

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.