0

i have a foreach loop to print a loop through a product collection

foreach ($productCollection->getItems() as $_product): ?>
                    <li>
                    <div style="float:left;width:300px;margin-right:20px;">
                        <div class="product-image">
                            <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(161,123); ?>" width="161" height="123" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
                        </div>
                        <div class="product-info">
                            <h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h3>
                            <?php if($isShowDescription == 1):?>
                                <div class="desc std">
                                    <?php $_description = nl2br($_product->getShortDescription())?>
                                    <?php $short_desc = substr($_description, 0, 200);  ?>
                                    <?php if(substr($short_desc, 0, strrpos($short_desc, ' '))!='') 
                                        $short_desc = substr($short_desc, 0, strrpos($short_desc, ' '));
                                        $short_desc = $short_desc.'...';    
                                    ?>
                                    <?php echo $short_desc;?>
                                </div>
                            <?php endif; ?>
                            <?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; ?>
                            </div>      
                        </div>
                    </div>                  
                <?php endforeach?>

This only print one product each time, how to make it that can print 3 products each time then go to next loop?

0

2 Answers 2

0

you need to use array_chunk for this && check this

3
  • $collArray = $productCollection->toArray(); not work.
    – hkguile
    Commented Jan 13, 2015 at 8:01
  • the answer not work for my case
    – hkguile
    Commented Jan 13, 2015 at 8:02
  • try this $productCollection->getItems();
    – sunel
    Commented Jan 13, 2015 at 9:04
0

Instead of foreach ($productCollection->getItems() as $_product): ?> use foreach ($productCollection as $_product): ?>

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