magento show number of products in the category?

magento show number of products in the category?

September 29, 2026 · Illustrated author avatarBy Stefan Bogdanescu
magento show number of products in the category?

Displaying Product Counts in Magento Categories

If you’re new to Magento and looking to display the number of products within each category (including sub-categories) on your store’s sidebar, you’ve come to the right place. Let me guide you through this process.

Understanding Your Current Setup

You’ve already made progress by creating a left_nav.phtml file in the correct location: app/design/frontend/my-theme/default/template/catalog/navigation/. This is where Magento looks for custom navigation templates.

Your current code, as shared on Pastebin, pulls all categories and sub-categories using $this->getCategoryCollection(). It’s a good start, but we need to modify it to count the products in each category.

Counting Products in Each Category

To display the product count, you’ll need to use Magento’s category collection and join it with the catalog_category_product table. This table contains associations between categories and their assigned products. Here’s how you can do this:

  1. Modify your code by replacing $this->getCategoryCollection() with the following:
$collection = $this->_categoryFactory->create();
$treeModel = new \Magento\Catalog\Model\Category\Tree();
$data = $treeModel->load()->getNodesById($collection->getAllIds());
  1. Iterate through each category and count its associated products:
foreach ($data as $category) {
    $productCollection = $this->_productFactory->create();
    $productCollection->addCategoryFilter($category->getId());

    // Get the number of associated products
    $productCount = $productCollection->getSize();

    // Output category name with product count
    echo "<li>" . $category->getName() . " (" . $productCount . ")</li>";
}

Don’t forget to replace _categoryFactory and _productFactory with the actual factory names (e.g., \Magento\Catalog\Model\CategoryFactory).

Handling Sub-categories

The above code will display all categories at the same level. If you want to include sub-categories, you can modify the $treeModel->load() call as follows:

$treeModel = new \Magento\Catalog\Model\Category\Tree();
$data = $treeModel->load()->getNodesById($collection->getAllIds(), true);

The true parameter tells the tree model to include sub-categories.

Conclusion

In this guide, we modified the existing navigation template to display both categories and their product counts. We joined the category collection with the appropriate table to count associated products for each category.

Remember, this is a customization that overrides Magento’s core behavior. Make sure to test your changes thoroughly on your development environment before deploying them to production.

For more advanced customizations, consider using Magento’s built-in UI components or exploring theme inheritance and layout updates. You can find detailed guides on these topics at LaravelCompany.

Don’t hesitate to reach out if you have any further questions!

Catalog Database Themes

Our network