0

A former colleague has made a custom template for magento however it is listing the products for a category twice. I've been asked to fix it although I know next to nothing about magento...

The template file in question appears to be

app/design/front/end/default/custom-theme/template/catalog/product/list.phtml

This file has what appears like a simple foreach loop to display the products

<?php foreach ($_productCollection as $_product): ?>

$_productCollection->count() returns 6 and on the front end page it says 6 Item(s) and there are 6 products displayed however they are displayed twice. I've tried fiddling with the foreach loops but without improvement. I've also tried comparing app/design/front/end/default/custom-theme/layout/catalog.xml with the one in a template that works but I don't know what I'm doing in there and it made no difference anyway.

Can anyone point me in the right direction? Is it simple?

Thanks!

edit - adding in list.phtml

<?php
$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
?>

<?php if(!$_productCollection->count()): ?>
<div class="note-msg-empty-catalog">
    <h3><?php echo $this->__('There are no products matching the selection.') ?></h3>
    <?php echo $this->__('This is a custom CMS block displayed if category is empty.') ?><br />
    <?php echo $this->__('You can replace it with any custom content.') ?>

</div>
<?php else: ?>
<div class="category-products">
<?php echo $this->getToolbarHtml() ?>
<?php // List mode ?>
<?php if($this->getMode()!='grid'): ?>
<?php $_iterator = 0; ?>
<ol class="products-list" id="products-list">
<?php foreach ($_productCollection as $_product): ?>
    <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
        <?php // Product Image ?>
        <?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
                    <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a></h2>

        <?php // Product description ?>
        <div class="product-shop">
            <div class="f-fix">
                <div class="list-left">
                <?php echo $this->getPriceHtml($_product, true) ?>
                    <?php if($_product->getRatingSummary()): ?>
                        <?php echo $this->getReviewsSummaryHtml($_product) ?>
                    <?php endif; ?>
                                            <!--<div class="desc std">
                        <?php //echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                        <a href="<?php //echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped ?>" class="link-learn"><?php //echo $this->__('Learn More') ?></a>
                    </div>-->
                </div>
                <div class="list-right">

                    <?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; ?>
                    <ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a title="<?php echo $this->__('Add to Wishlist') ?>" href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist tooltips"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                            <li><span class="separator">|</span> <a title="<?php echo $this->__('Add to Compare') ?>" href="<?php echo $_compareUrl ?>" class="link-compare tooltips"><?php echo $this->__('Add to Compare') ?></a></li>
                        <?php endif; ?>
                    </ul>
                </div>
                <div class="clear"></div>
            </div>
        </div>
        <?php $_product = Mage::getModel('catalog/product')->load($_product->getId()); ?>
        <div class="label-product">             
            <?php if($_product->getData('new')){echo '<span class="new">'.$this->__('New').'</span>';  }?>
            <?php if($_product->getData('sale')){echo '<span class="sale">'.$this->__('Sale').'</span>';  }?>
        </div>

    </li>
<?php endforeach; ?>
</ol>
<script type="text/javascript">decorateList('products-list', 'none-recursive')</script>

<?php else: ?>

<?php // Grid Mode ?>

<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = 4; ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
    <?php if ($i++%$_columnCount==0): ?>
    <ol class="products-list" id="products-list">
<?php foreach ($_productCollection as $_product): ?>
    <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
        <?php // Product Image ?>
        <?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
                    <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a></h2>

<?php

                             $_quantity = $this->htmlEscape($_product->getQuantity());



                                  if ($_quantity < 1)
                                  $_quantitystr =  " ";
                                  else
                                  $_quantitystr = "Qty: " . $_quantity;
                                  echo $_quantitystr;

?> 

        <?php // Product description ?>
        <div class="product-shop">
            <div class="f-fix">
                <div class="list-left">
                <?php echo $this->getPriceHtml($_product, true) ?>
                    <?php if($_product->getRatingSummary()): ?>
                        <?php echo $this->getReviewsSummaryHtml($_product) ?>
                    <?php endif; ?>
                                            <!--<div class="desc std">
                        <?php //echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                        <a href="<?php //echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped ?>" class="link-learn"><?php //echo $this->__('Learn More') ?></a>
                    </div>-->
                </div>
                <div class="list-right">

                    <?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; ?>
                    <ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a title="<?php echo $this->__('Add to Wishlist') ?>" href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist tooltips"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                            <li><span class="separator">|</span> <a title="<?php echo $this->__('Add to Compare') ?>" href="<?php echo $_compareUrl ?>" class="link-compare tooltips"><?php echo $this->__('Add to Compare') ?></a></li>
                        <?php endif; ?>
                    </ul>
                </div>
                <div class="clear"></div>
            </div>
        </div>
        <?php $_product = Mage::getModel('catalog/product')->load($_product->getId()); ?>
        <div class="label-product">             
            <?php if($_product->getData('new')){echo '<span class="new">'.$this->__('New').'</span>';  }?>
            <?php if($_product->getData('sale')){echo '<span class="sale">'.$this->__('Sale').'</span>';  }?>
        </div>

    </li>
<?php endforeach; ?>
</ol>
    <?php endif ?>
    <?php endforeach ?>
    <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
<?php endif; ?>

<div class="toolbar-bottom">
    <?php echo $this->getToolbarHtml() ?>
</div>

  • paste your whole .phtml file code here in question. – Siddharth Vaghasia Feb 12 '15 at 10:27
1

I'm guessing that you're using the grid mode when you get the products twice? In line the 6th line of Grid Mode you have second foreach loop initiated that caused your troubles.

it should look like

<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = 4; ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
    <?php if ($i++%$_columnCount==0): ?>
    <ol class="products-list" id="products-list">
<?php endif; ?> //this is the substitution
    <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">

In the original file there is an endif in its place. You'd also have to eliminate the second endforeach at the bottom and add a last endif. I tried it and it worked fine.

        <?php $_product = Mage::getModel('catalog/product')->load($_product->getId()); ?>
        <div class="label-product">             
            <?php if($_product->getData('new')){echo '<span class="new">'.$this->__('New').'</span>';  }?>
            <?php if($_product->getData('sale')){echo '<span class="sale">'.$this->__('Sale').'</span>';  }?>
        </div>

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

<?php endif; ?> //include this as well
<div class="toolbar-bottom">
    <?php echo $this->getToolbarHtml() ?>
</div>
  • This seems to have done the trick, thanks a lot! – Shane Feb 12 '15 at 14:30

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.