I want to show category product count which is shown in admin after the category name. As of now I write this code but it's not working properly.


Please see the image to get better idea.

Magento Backend

Please see the image to get better idea.

Magento Frontend

enter image description here


Not sure if this is still relevant but answering it anyway. The answer post by @Jean Paul CP is correct. $category->getProductCount(); return exactly the count which shown in the admin panel categories listing. On the other hand $category->getProductCollection()->count(); only returns count of active saleable products in the category.


To get the current category product count

Create your own module at below location Magento_root/app/code for that create Package/Modulename

Create below files there






Put this code in your Blockfile.php

namespace Package\Module\Block;

class Blockfile extends \Magento\Framework\View\Element\Template{
public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
            \Magento\Framework\Registry $registry
    ) {
        $this->_registry = $registry;
    public function getCurrentCategory(){
        $current_Category = $this->_registry->registry('current_category');
        return $current_Category;

& this code in your template.phtml

    $current_Category = $block->getCurrentCategory();
echo $current_Category->getName().'('.$current_Category->getProductCollection()->Count().')';
$categories = $this->getStoreCategories(true,false,true);
    $subCategories = $current_Category->getChildrenCategories($categories);
      foreach($subCategories as $subCategory) {
         echo '<li style="color:Green;"><a href="'.$subCategory->getUrl().'">'.$subCategory->getName().'(' .$subCategory->getProductCollection()->Count().')'.'</a></li>';

Run The below Command & see the changes

php bin/magento setup:upgrade

Still facing any issue feel free to revert back


It is working fine for me you can have a look


enter image description here


enter image description hereenter image description hereenter image description hereenter image description here

Note: Try to avoid the use of objectmanager into the template files

  • Hi Thanks for the code, But my requirement is not for current category product collection count. – Ravi Soni Feb 22 '18 at 12:50
  • What is your requirement? – Abhishek Tripathi Feb 22 '18 at 12:51
  • I want the count which is show in the category name bracket, Actually I debug the code and that count comes from the Magento\Catalog\Block\Adminhtml\Category\Tree.php Function: buildNodeName($node), You can check to get more Idea. – Ravi Soni Feb 22 '18 at 13:18
  • See my updated answer – Abhishek Tripathi Feb 22 '18 at 13:30
  • Code working fine, But still you check my question for M20 Mixer(140) admin and still here it is M20 Mixer(20). Still debug..... – Ravi Soni Feb 22 '18 at 13:55

This one using objectmanager. You can also inject the CategoryFactory and use it. As per your requirement you need to pass the category ID in foreach to achieve this.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$cateinstance = $objectManager->create('Magento\Catalog\Model\CategoryFactory');
$allcategoryproduct = $cateinstance->create()->load($cateid)->getProductCollection()->addAttributeToSelect('*');
echo $count = $allcategoryproduct->count();
  • Hi Thanks for the code, But same issue still there. – Ravi Soni Feb 22 '18 at 12:04
  • What issue? Is the count wrong? – Jeeva Chezhiyan Feb 22 '18 at 12:42
  • Yes, count wrong, you can see the count in my Question. – Ravi Soni Feb 22 '18 at 12:52
  • I think you have written like only for visible products. Because the remaining category counts are correct. – Jeeva Chezhiyan Feb 22 '18 at 12:57
  • My all products are Catalog, Search , So no need to make any changes there. – Ravi Soni Feb 22 '18 at 13:27
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
$product_count = $category->getProductCount();
  • Please do not post answers with code only. Explain what the code is doing and how it will solve the OP's issue – 7ochem May 12 '18 at 22:20

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.