0

This is the display setting of one of a category that is listing all products instead of showing only products that are assigned to it.

Display Mode = Products Only
CMS Block = Please select a static block ...
Is Anchor = No

edit:

Category Products tab has only 1 product assigned to it.

edit2:

this is the code that is showing all the products: (app/design/frontend/base/default/template/catalog/category/view.phtml)

<div class="category-products">
<ul class="products-grid">

<?php
//echo $_category->getId();
$_cat = new Mage_Catalog_Block_Navigation();
$curent_cat = $_cat->getCurrentCategory();
$curent_cat_id = $curent_cat->getId();
$parentId=Mage::getModel('catalog/category')->load($curent_cat_id)->getParentId();

$categoryid = $parentId;//$_category->getId();

$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');

//echo "<pre>";print_r($collection);echo "</pre>"; die();

$i=0; foreach ($collection as $_product) { ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<a href="<?php echo $_product->getProductUrl(); ?>"><img class="product-image" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" alt="" /></a> <h2 class="product-name"><a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a></h2>
<?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; ?>
                    <ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                            <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
                        <?php endif; ?>
                    </ul>
                </div>
<?php $i++; } ?>
</li>
<?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
</ul>
 <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
  <div class="toolbar-bottom">
        <?php echo $this->getToolbarHtml() ?>
    </div>
<?php endif ?>

</div>
  • did you try to rebuild your indexes and clear the cache? – Marius May 7 '13 at 21:10
  • cache is disabled. – Aamir Siddique May 7 '13 at 21:13
  • not sure how to rebuild indexes. Btw, one thin i've noticed, i.e. when i create new category with no products assigned, it also shows all the products from all the categories. – Aamir Siddique May 7 '13 at 21:14
  • System->Index management. Select all, rebuild index. It may take a while. – Marius May 7 '13 at 21:17
  • tried, but no use. – Aamir Siddique May 7 '13 at 21:24
1
  1. Don't use new, Magento has factory functions to allow rewriting of components:

    • Mage::getModel()
    • Mage::getSingleton()
  2. If I understand your code correct ($categoryid = $parentId;) you are iterating over the parent category. Remove this line and it should work.

  3. Your code is messy and contains lots of things which are better placed in the block class. Have a look into the original code.

  4. Don't edit frontend/base/default and adminhtml/default/default. These are base templates. If you want to make modifications, you need to create your own theme, copy the templates you want to change to frontend/yourThemeName/default, e.g. frontend/yourThemeName/default/catalog/category/view.phtml and edit it there. Then activate your theme in the backend System > Config > Design > Package(?) > yourThemeName.

  • thank you for the code, i still the see all products after removing $categoryid = $parentId;. But if i replace it with this line $categoryid = $_category->getId(); then i see fine but this change affects all categories that have sub-categories and shows no products at all. – Aamir Siddique May 8 '13 at 16:12
  • I have just realized that if a category has sub-categories, then it works fine and do not show all products. But if a category has no sub-categories then it shows all products. – Aamir Siddique May 8 '13 at 16:15
  • It sounds like your template is totally broken :) – Fabian Blechschmidt May 8 '13 at 16:33
  • ok, how can i find out if a category has any child categories? thx – Aamir Siddique May 8 '13 at 16:40
  • in the template? $this->getCategory()->getChildren() and mybe there is a method hasChildren() But I recommend to have a look on the original template and fix the mess! – Fabian Blechschmidt May 8 '13 at 16:45

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.