How To

Magento 2: How To Display Count Of Product with Page Title on Category Page

Hello Magento Folks 👋,

Hope you all are doing great. Today I will show Magento 2: How To Display Count Of Product with Page Title on Category Page. Let’s not miss our last published blog, Magento 2: Add Track Order link with the Order on Order History Page.

By default, Magento 2 offers some default functionality to display the count of no. of products for a particular category on the category page with the toolbar.

But what if we want to display a count of no. of products as per our essentiality? No worries, with the help of the below code it is possible to display the count of products for a particular category as per our needs.

Here we have used display count with page main title. So, let us do 🚀

Steps to Display Count Of Product with Page Title on Category Page in Magento 2:

Step 1: To do this, create events.xml file inside  app\Code\Vendor\Extension\etc\ folder and add the below code:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="layout_generate_blocks_after">
        <observer instance="Vendor\Extension\Observer\Productcount"
                  name="Vendor\Extension\Observer\Productcount"/>
    </event>
</config>

Step 2: Next, create Productcount.php inside app\Code\Vendor\Extension\Observer\ folder and add the below code:

<?php
namespace Vendor\Extension\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class Productcount implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        if ($observer->getEvent()->getData('full_action_name') === 'catalog_category_view')
        {
            $layout = $observer->getEvent()->getData('layout');
            $listBlock = $layout->getBlock('category.products.list');
            $categoryViewBlock = $layout->getBlock('category.products');
            $count  = 0;
            if ($listBlock) 
            {
                $productCollection = $listBlock->getLoadedProductCollection();
                if ($productCollection && $productCollection->count() >0)
                { 
                    $count = $productCollection->count();
                }
            }
            if ($categoryViewBlock) 
            {
                $currentCategory = $categoryViewBlock->getCurrentCategory();
                if ($currentCategory)
                {    
                    $count = $currentCategory->getProductCollection()->count();
                }
            }
            $pageMainTitle = $layout->getBlock('page.main.title');
            if ($pageMainTitle)
            {
                $oldTitle =$pageMainTitle->getPageTitle();
                $pageMainTitle->setPageTitle(
                    $oldTitle
                    . _(' - Product Count: ' . (int) $count)
                );
            }
        }
    }
}

Step 3: Finally run the below command:

php bin/magento cache:flush

That’s it.

Conclusion:

Accordingly, all are able to Display Count Of Product with Page Title on Category Page in Magento 2. If you face any complications while executing the above code, write to me in the comment section below. Help your fellow Magento friends by sharing the code with them. See you again, until then stay tuned!

Happy Coding 😊

Click to rate this post!
[Total: 7 Average: 4.9]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • I completed the code addition according to this, but there is no change in the foreground
    can you check it for me

    • Confirm you also created a required file for an extension, and your extension is registered and enabled in the system.

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

1 day ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

4 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

4 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

4 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

5 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago