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

6 Answers 6


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, 2018 at 12:50
  • What is your requirement? Feb 22, 2018 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, 2018 at 13:18
  • See my updated answer Feb 22, 2018 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, 2018 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, 2018 at 12:04
  • What issue? Is the count wrong? Feb 22, 2018 at 12:42
  • Yes, count wrong, you can see the count in my Question.
    – Ravi Soni
    Feb 22, 2018 at 12:52
  • I think you have written like only for visible products. Because the remaining category counts are correct. Feb 22, 2018 at 12:57
  • My all products are Catalog, Search , So no need to make any changes there.
    – Ravi Soni
    Feb 22, 2018 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, 2018 at 22:20
  • It is not recommended to use ObjectManager directly. We should use CategoryResourceModel to query the DB.
    – Harish ST
    May 16, 2021 at 14:42

Take a look at this:


    $productsCollection = $category->getProductCollection();
    $productsCollection = $this->stockProcessor->process($productsCollection, $this->searchCriteria, []);

    return $productsCollection->getSize();
  • This method will not count the products of child category
    – huykon225
    Jun 1, 2021 at 2:46
  • I haven't had a chance to test. Did you check isAnchor @huykon225? Jun 1, 2021 at 3:12
  • Yes I check. I have seen the different between magento default with graphql. The graphql return without count the products of child categories
    – huykon225
    Jun 1, 2021 at 3:22

Admin panel uses:


This flag adds products count to categories in collection. Use the flag before load(). It counts products of child categories too.

For example, I want to get products count for categories 4 and 5:


Also you can use next method of the collection:

loadProductCount($items, $countRegular = true, $countAnchor = true)

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.