how can I get all product of a category in a custom page in magento 1.9
-
14-02-2021 - |
Question
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>
Solution
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>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange