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 🚀
Contents
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.
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 😊
In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…
In the world of business, understanding financial metrics is crucial for making informed decisions and…
Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…
In this era, businesses are always on the lookout for ways to engage with their…
Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…
E-commerce has transformed the way consumers shop for products and services and interact with businesses.…
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.