1

I want to display product count with page main title for category page.

How I can display that?

enter image description here

Thanks.

2
  • Can you describe this in details. Sep 29, 2020 at 10:50
  • @DhirenVasoya i added screenshot. i want to add count like that.
    – Niket
    Sep 29, 2020 at 13:11

3 Answers 3

2

Using Observer over Event layout_generate_blocks_after you can show product count at title

Define an observer at events.xml

events.xml:

<?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="StackExchange\SampleModule\Observer\Frontend\Layout\GenerateBlocksAfter"
                  name="stackexchange_layout_generateblocksafter_layout_generate_blocks_after"/>
    </event>
</config>

Observer Class:

<?php
declare(strict_types=1);

namespace StackExchange\SampleModule\Observer\Frontend\Layout;

class GenerateBlocksAfter implements \Magento\Framework\Event\ObserverInterface
{
    /**
     * @var \Magento\Framework\View\Page\Config
     */
    private $config;

    public function __construct(
        \Magento\Framework\View\Page\Config $config
    ) {
        $this->config = $config;
    }

    /**
     * Execute observer
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {
        $fullActionName = $observer->getEvent()->getData('full_action_name');
        /**
         * @var $layout \Magento\Framework\View\LayoutInterface
         */
        $layout = $observer->getEvent()->getData('layout');
        if ($fullActionName === 'catalog_category_view') {
            $listBlock = $layout->getBlock('category.products.list');
            $categoryViewBlock = $layout->getBlock('category.products');
            $count  = 0;
            if ($listBlock) {
                /**
                 * @var $listBlock \Magento\Catalog\Block\Product\ListProduct
                 */
                $productCollection = $listBlock->getLoadedProductCollection();
                if ($productCollection && $productCollection->count() >0) {
                    /**
                     * use this code when you want the same count as layer navigation
                     *
                     */
                    $count = $productCollection->count();
                }
            }

            if ($categoryViewBlock) {

                /**
                 * @var $categoryViewBlock \Magento\Catalog\Block\Category\View
                 */
                $currentCategory = $categoryViewBlock->getCurrentCategory();
                if ($currentCategory) {
                    /**
                     * use this code when you want show count before applying filter
                     */
                    $count = $currentCategory->getProductCollection()->count();
                }
            }
            $title = $this->config->getTitle()->get();
            $this->config->getTitle()->set(
                $title
                . _(' Product Count: ' . (int) $count)
            );
        }
    }
}

Use $count = $productCollection->count(); when want same result at Laynavigation .

And use $count = $currentCategory->getProductCollection()->count(); when you always show category product count even applying filter.

The folder structure of the module

enter image description here

Update

If you want to update 'page.main.title' then use below code:

<?php
declare(strict_types=1);

namespace StackExchange\SampleModule\Observer\Frontend\Layout;

class GenerateBlocksAfter implements \Magento\Framework\Event\ObserverInterface
{
    /**
     * @var \Magento\Framework\View\Page\Config
     */
    private $config;

    public function __construct(
        \Magento\Framework\View\Page\Config $config
    ) {
        $this->config = $config;
    }

    /**
     * Execute observer
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {
        $fullActionName = $observer->getEvent()->getData('full_action_name');
        /**
         * @var $layout \Magento\Framework\View\LayoutInterface
         */
        $layout = $observer->getEvent()->getData('layout');
        if ($fullActionName === 'catalog_category_view') {
            $listBlock = $layout->getBlock('category.products.list');
            $categoryViewBlock = $layout->getBlock('category.products');
            $count  = 0;
            if ($listBlock) {
                /**
                 * @var $listBlock \Magento\Catalog\Block\Product\ListProduct
                 */
                $productCollection = $listBlock->getLoadedProductCollection();
                if ($productCollection && $productCollection->count() >0) {
                    /**
                     * use this code when you want same count as layer navigation
                     *
                     */
                    $count = $productCollection->count();
                }
            }

            if ($categoryViewBlock) {

                /**
                 * @var $categoryViewBlock \Magento\Catalog\Block\Category\View
                 */
                $currentCategory = $categoryViewBlock->getCurrentCategory();
                if ($currentCategory) {
                    /**
                     * use this code when you want show count before applying filter
                     */
                    $count = $currentCategory->getProductCollection()->count();
                }
            }

            /**
             * @var $pageMainTitle \Magento\Theme\Block\Html\Title
             */
            $pageMainTitle = $layout->getBlock('page.main.title');
            if ($pageMainTitle) {
                $oldTitle =$pageMainTitle->getPageTitle();
                $pageMainTitle->setPageTitle(
                    $oldTitle
                    . _(' Product Count: ' . (int) $count)
                );
            }
        }
    }
}
4
  • It's not working. Actually it's page.main.title. How can i set?
    – Niket
    Sep 30, 2020 at 7:28
  • Thanks..!! it's working.
    – Niket
    Sep 30, 2020 at 8:29
  • Amit one more question. It's. not update when filter collection. what should i need to do?
    – Niket
    Sep 30, 2020 at 8:41
  • Please read the comment on code and comment $count = $currentCategory->getProductCollection()->count();
    – Amit Bera
    Sep 30, 2020 at 9:08
0

Let me share a basic idea for this task.

You can achieve this using the below code in your list page template file. Please use proper dependency instead of objectmanager

<?php        
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category'); echo $category->getProductCollection()->count();
2
  • Can you please tell me in which file should i need to add?
    – Niket
    Sep 29, 2020 at 13:07
  • please check my updated question also.
    – Niket
    Sep 29, 2020 at 13:14
0

We can show the total number of product counts on the PLP page without using events. All we need to do is override the catalog_category_view.xml file and make the change accordingly. Below I'm posting the code which is worked for me.

catalog_category_view.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2018 Porto. All rights reserved.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <move element="page.main.title" destination="category.title"/>
        <referenceContainer name="content">
            <container name="category.title" htmlTag="div" htmlClass="category-title">
                <block class="Magento\Catalog\Block\Category\View" name="category.products.count"
                       as="product_count" template="Magento_Catalog::product/category_title.phtml" />
            </container>
        </referenceContainer>
    </body>
</page>

category_title.phtml

<span class="category-product-count">
<?= '('.$this->getCurrentCategory()->getProductCollection()->count().' Results)'; ?>

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.