0

I am creating navigation for my store. I am new to magento. I am having problem to load product under that category. It is possible for me to print all product under all categories but I dont know how to load products under its specific category.

This is my code

<?php $_menu = $this->renderCategoriesMenuHtml($this->getSelectedCategoryId(), 0, 'level-top') ?>
<?php
if ($this->getTitle() != '') {
    $title = $this->getTitle();
} else {
    $title = 'Categories';
}
?>
<?php
$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
?>
<?php if ($_menu): ?>
        <div class="block vertical-menu">   
            <div class="title-block" style="margin-bottom:0px;"><h4><?php echo $this->__($title) ?></h4></div>  
            <div class="block-content">
                <?php $children = Mage::getModel('catalog/category')->getCategories(2);?>
                <ul class="content-navigation">
                    <?php foreach($children as $category){ ?>
                    <li>
                    <a href="<?php echo $category->getUrlPath(); ?>"><?php echo $category->getName();?></a>
                    <?php $category = Mage::getModel('catalog/product')->load($category->getId()); //Pass category ID here.
                          $productCollection = $category->getProductCollection();?>

                    <ul>
                        <?php foreach($productCollection as $product){ ?>
                        <li><a href="#"><?php echo $product->getName(); ?></a></li>
                        <?php }?>
                    </ul>
                </li>
                    <?php } ?>
                </ul>
            </div>
        </div>
<?php else: ?>
    <?php if (Mage::helper('mpanel')->acceptToUsePanel()): ?>
        <div class="block vertical-menu">   
            <div class="block-title"><strong><span><?php echo $this->__($title) ?></span></strong></div>    
            <div class="block-content">
                <p><?php echo $this->__('Have no categories.'); ?></p>
            </div>
        </div>
    <?php endif; ?>
<?php endif; ?>

EDIT

<div class="block-content">
                <?php $children = Mage::getModel('catalog/category')->getCategories(2);?>
                <ul class="content-navigation">
                    <?php foreach($children as $category){ ?>
                    <li>
                    <a href="<?php echo $category->getUrlPath(); ?>"><?php echo $category->getName();?></a>
                    <?php $categoryId = $category->getId(); ?>
                    <?php $category = Mage::getModel('catalog/category')->load($category_id);
                            $products = Mage::getModel('catalog/product')
                            ->getCollection()
                            ->addAttributeToSelect('*')
                            ->addCategoryFilter($category)
                            ->load();
                    ?>
                    <ul>
                        <?php foreach($products as $product){ ?>
                        <li><a href="#"><?php echo $product->getName(); ?></a></li>
                        <?php }?>
                    </ul>
                </li>
                    <?php } ?>
                </ul>
            </div>
4
  • you need to print product of particular category, right? Commented Nov 8, 2016 at 10:16
  • yes... correct... inside navigation
    – parish
    Commented Nov 8, 2016 at 10:19
  • I added answer how to fetch category product, you can used that code, and it help then mark as solution so other get help. Commented Nov 8, 2016 at 10:25
  • I update answer, please check. Commented Nov 8, 2016 at 10:56

2 Answers 2

1

You can used this code to load product collection of particular category.

$categoryId = 32; // Replace with you category Id    
$category = Mage::getModel('catalog/category')->load($categoryId);
$products = Mage::getResourceModel('catalog/product_collection')
          ->addCategoryFilter($category)
          ->addAttributeToSelect('*');

Try this code.

9
  • Check my edit .... It prints all product i have...
    – parish
    Commented Nov 8, 2016 at 10:44
  • @parish update code properly, so i will check. Commented Nov 8, 2016 at 10:47
  • Updated my code please check
    – parish
    Commented Nov 8, 2016 at 10:50
  • I update my answer, please try this code. Commented Nov 8, 2016 at 10:53
  • $categoryId and $category_id needs to be same
    – parish
    Commented Nov 8, 2016 at 10:59
0

Following code will give you all the enabled products of the required category. If you need both enabled and disabled products then remove the filter of status from the collection.

$categoryId = '4' //Your category Id

$products = Mage::getModel('catalog/category')->load($categoryId)
         ->getProductCollection()
         ->addAttributeToSelect('*')
         ->addAttributeToFilter('status', 1)
         ->addAttributeToFilter('visibility', 4)
         ;
       foreach($products as $product)
       {
        echo 'Product Name: ' . $product->getName() . '<br/>';
       }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.