2

I've built a new block that displays products placed in a specific category (chosen via ID) and renders them out with some fancy jQuery. Now to test this I passed a id of 17, a category with plenty of products 10+. All works fine, products are found and rendered out propperly.

If i however pass it the id for a root category (I have called featured products) that has 6 products in, it fails to work.

    <!-- Global Variables -->
<?php $category = Mage::getModel('catalog/category'); ?>
<?php $_helper  = $this->helper('catalog/output'); ?>

<?php 
$featured_category  = Mage::getModel('catalog/category')->load(17);
$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addCategoryFilter($featured_category)
    ->addAttributeToSelect("*")
    ->load();
?>
        <ul>
        <?php $counter = 0; ?>
        <?php foreach($products as $_item): ?>

Is there anything major I've done wrong that would stop this from working in a root category but not a sub category?

  • What is your root category ID? If its 0 or 1 - then perhaps its being passed as a boolean, not integer – Ben Lessani - Sonassi Mar 7 '13 at 10:05
  • How did you go with this? If you found a solution then please post your answer. – jharrison.au Mar 9 '13 at 13:58
  • I've tested your code myself and it works fine provided the root category has "Is Anchor" set to "No". Can you double check this is the case for you? – jharrison.au Mar 13 '13 at 1:56
4

I think you may be approaching this from the wrong angle. There is no need to create a new block type, the default Mage_Catalog_Block_Product_List contains all the logic you need within the _getProductCollection() method

Showing root category

CMS method

{{block type="catalog/product_list" name="featured.products.list" show_root_category="true" template="catalog/product/list.phtml"}}

Layout XML method

<block type="catalog/product_list" name="featured.products.list" show_root_category="true" template="catalog/product/list.phtml"/>

Showing a nominated category

CMS method

{{block type="catalog/product_list" name="featured.products.list" category_id="17" template="catalog/product/list.phtml"}}

Layout XML method

<block type="catalog/product_list" name="featured.products.list" category_id="17" template="catalog/product/list.phtml"/>

From within any product list block

$this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
$items = $this->getLoadedProductCollection();

That's all that is needed. Look at Mage_Catalog_Block_Product_List::_getProductCollection() to see what's happening behind the scenes.

| improve this answer | |
  • The method isn't my issue, I am simply trying to get the products from the root category a special way for knowing how to do it's sake. – Chris Morris Mar 7 '13 at 12:17
  • Updated my answer with the code for your situation – jharrison.au Mar 7 '13 at 20:29

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.