0

I have created a custom sub category page, in which I'm calling all the sub-categories, now i want to show product of each sub-categories in the same page how can I do that.

This is what I do to get my sub-categories

1 - Create a custom block "sub-category-listing"

2 - Then I have created a subcategorylist.phtml page

<div class="category-products sub-cat">
<ul class="products-grid" style="margin-left:0px;margin-right:0px;">
<?php
$_categories=$this->getCurrentChildCategories();
if($_categories->count()):
$categorycount = 0;
foreach ($_categories as $_category):
if($_category->getIsActive()):
$cur_category=Mage::getModel('catalog/category')->load($_category- 
>getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
$catName = $this->getCurrentCategory()->getName();

?>

<li class="item <?=$class?>">
<h2><a href="<?php echo $_category->getURL() ?>" title="<?php echo 
$this->htmlEscape($_category->getName()) ?>"><?php echo $this- 
>htmlEscape($_category->getName()) ?></a></h2>
</li>
<?php
endif;
if($categorycount == 100000){
$categorycount = 0;
echo "</ul>\n\n<ul class=\"products-grid\">";
}
else{
$categorycount++;
}
endforeach;
endif;
?>
</ul>
</div>
  • does this work for you? – Aasim Goriya Jan 18 '19 at 11:49
  • no it doesn't but thanks anyway – abhishek kalotra Jan 19 '19 at 5:13
  • are you getting any error ? – Aasim Goriya Jan 22 '19 at 9:23
  • no, its not showing anything – abhishek kalotra Jan 29 '19 at 8:12
  • did you get the list of categories > – Aasim Goriya Jan 29 '19 at 8:18
1

Please update your code as per below to show all your sub category products.

<div class="category-products sub-cat">
    <ul class="products-grid" style="margin-left:0px;margin-right:0px;">
        <?php
        $_categories=$this->getCurrentChildCategories();
        if($_categories->count()):
            $categorycount = 0;
        foreach ($_categories as $_category):
            if($_category->getIsActive()):
                $cur_category=Mage::getModel('catalog/category')->load($_category- 
                    >getId());
            $layer = Mage::getSingleton('catalog/layer');
            $layer->setCurrentCategory($cur_category);
            $catName = $this->getCurrentCategory()->getName();

            ?>

            <li class="item <?=$class?>">
                <h2><a href="<?php echo $_category->getURL() ?>" title="<?php echo 
                $this->htmlEscape($_category->getName()) ?>"><?php echo $this- 
                >htmlEscape($_category->getName()) ?></a></h2>
            </li>
            <?php
            endif;
            if($categorycount == 100000){
                $categorycount = 0;
                echo "</ul>\n\n<ul class=\"products-grid\">";
            }
            else{

                /* For Fetching product by category id */
                $categoryid = $_category->getId();

                $category = Mage::getModel('catalog/category');
                $category->load($categoryid);
                $collection = $category->getProductCollection();
                $collection->addAttributeToSelect('*');

                foreach ($collection as $_product) { ?>
                <li class="product-item">
                    <a href="<?php echo $_product->getProductUrl() ?>">
                        <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" />
                    </a> 
                    <a href="<?php echo $_product->getProductUrl(); ?>"><?php echo $_product->getName(); ?></a>
                </li>
                <?php } 
                $categorycount++;
            }
            endforeach;
            endif;
            ?>
        </ul>
    </div>
| improve this answer | |

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.