Site icon MageComp Blog

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

How To Display Count Of Products With Page Title On Category Page in Magento 2

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 ?

Exit mobile version