Skip to main content

Magento 1: get all categories with product count

 <?php $cat_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); ?>
                    <?php $category = Mage::getModel('catalog/category')->load($cat_id); ?>
                    <?php //echo $category->getName(); ?>
                   
                    <?php $_helper = Mage::helper('catalog/category') ?>
                    <?php $_categories = $_helper->getStoreCategories() ?>
                    <?php if (count($_categories) > 0): ?>
                        <ul>
                            <?php foreach($_categories as $_category): ?>
                                <li>
                                    <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                                        <?php echo $_category->getName() ?>-
                                        <?php echo $categoryId = $_category->getId() ?>
                                        <br>
                                        <?php echo $_category->getProductCount() ?>
                                        <?php
                                            //$categoryId = '4';
                                            echo $products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection()->getSize();  
                                        ?>
                                        <br>
                                        <?php $products_counts = Mage::getModel('catalog/category')->load($_category->getId())
 ->getProductCount();

echo($products_counts); ?>
                                        <pre>
                                        <?php print_r( $_category->getData() );?>
                                        </pre>
                                    </a>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>

Comments

Popular posts from this blog

Magento 2: Category list for custom magento system configuration section ( Backend )

In system.xml file field for multi select of category is like: NOTE: Use Select for Single item and multiselect for multiple in - <field id = "bannerlist" translate = "label" type = " multiselect " <group id = "bannerblock_setting" translate = "label" type = "text" delault = "1" sortOrder = "3" showInDefault = "1" showInWebsite = "1" showInStore = "1" > <label> Setting </label> <field id = "bannerlist" translate = "label" type = "multiselect" sortOrder = "10" showInDefault = "1" showInWebsite = "1" showInStore = "1" > <label> Select Category </label> <!-- <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>--> <source_model> Ipragmatech\Bannerblock\Model\Config\Source\C...

Magento 2: Get Products by category ID

<?php $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        // $appState = $objectManager->get('\Magento\Framework\App\State'); // $appState->setAreaCode('frontend'); $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory'); $categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category'); $categoryRepository = $objectManager->get('\Magento\Catalog\Model\CategoryRepository'); $store = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore(); $categoryId = 47; // YOUR CATEGORY ID $category = $categoryFactory->create()->load($categoryId); $categoryProducts = $category->getProductCollection()                              ->addAttributeToSelect('*');       ...