2

Which function is responsible for displaying the total count of products in the filters in the layered navigation.

ie) I need to get the count (eg 50 ) in my custom filterable attributes as well.

enter image description here

This is how I have overridden filter.phtml in my code \Vendor\LayeredNavigation\view\frontend\layout\catalogsearch_result_index.xml

<page layout="3columns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
    <referenceBlock class="Vendor\LayeredNavigation\Block\Navigation" before="-" name="catalogsearch.leftnav">
      <action method="setTemplate">
        <argument name="template" xsi:type="string">Vendor_LayeredNavigation::layer/view.phtml</argument>
      </action>
    </referenceBlock>
    
    <referenceContainer name="content">
        <block class="Magento\LayeredNavigation\Block\Navigation\FilterRenderer" name="catalogsearch.navigation.renderer" as="renderer" template="layer/filter.phtml"/>
    </referenceContainer>
</body>

di.xml

<preference for="Magento\LayeredNavigation\Block\Navigation\FilterRenderer" type="Vendor\LayeredNavigation\Block\Navigation\FilterRenderer" />

1 Answer 1

1

Go to vendor\magento\module-layered-navigation\view\frontend\templates\layer\filter.phtml

<ol class="items">
    <?php foreach ($filterItems as $filterItem): ?>
        <li class="item">
            <?php if ($filterItem->getCount() > 0): ?>
                <a href="<?php echo $block->escapeUrl($filterItem->getUrl()) ?>">
                    <?php /* @escapeNotVerified */ echo $filterItem->getLabel() ?>
                    <?php if ($this->helper('\Magento\Catalog\Helper\Data')->shouldDisplayProductCountOnLayer()): ?>
                        <span class="count"><?php /* @escapeNotVerified */ echo $filterItem->getCount()?><span class="filter-count-label">
                            <?php if ($filterItem->getCount() == 1):?> <?php /* @escapeNotVerified */ echo __('item')?><?php else:?> <?php /* @escapeNotVerified */ echo __('items') ?><?php endif;?></span></span>
                    <?php endif; ?>
                </a>
            <?php else:?>
                <?php /* @escapeNotVerified */ echo $filterItem->getLabel() ?>
                <?php if ($this->helper('\Magento\Catalog\Helper\Data')->shouldDisplayProductCountOnLayer()): ?>
                    <span class="count"><?php /* @escapeNotVerified */ echo $filterItem->getCount()?><span class="filter-count-label">
                        <?php if ($filterItem->getCount() == 1):?><?php /* @escapeNotVerified */ echo __('item')?><?php else:?><?php /* @escapeNotVerified */ echo __('items') ?><?php endif;?></span></span>
                <?php endif; ?>
            <?php endif; ?>
        </li>
    <?php endforeach ?>
</ol>

Below function will be main shouldDisplayProductCountOnLayer & getCount

<?php if ($this->helper('\Magento\Catalog\Helper\Data')->shouldDisplayProductCountOnLayer()): ?>
        <span class="count"><?php /* @escapeNotVerified */ echo $filterItem->getCount()?><span class="filter-count-label">
            <?php if ($filterItem->getCount() == 1):?> <?php /* @escapeNotVerified */ echo __('item')?><?php else:?> <?php /* @escapeNotVerified */ echo __('items') ?><?php endif;?></span></span>
<?php endif; ?>
9
  • do i need to override the filter phtm(Magento\LayeredNavigation\Block\Navigation\FilterRenderer class), as im not making any custom change to it?.. i have overridden only view.phtml for displaying my custom filter(Magento\LayeredNavigation\Block\Navigation)...but still why are the counts not displaying in custom filters in layered navigation?
    – Sushivam
    Commented Oct 18, 2016 at 9:45
  • Yup. You have to override in your theme. Try to clear cache, var & check again. It should work
    – Jackson
    Commented Oct 18, 2016 at 9:47
  • Could you pls check if my updated answer is correct? i mean is calling the block class rite? i get Undefined variable: filterItems in Magento cores filter phtml file
    – Sushivam
    Commented Oct 18, 2016 at 9:59
  • Ofcourse you need to get that Block file as well. Otherwise how will you get Array? So you need to copy filterItems related things from Core & Override in ur once
    – Jackson
    Commented Oct 18, 2016 at 10:01
  • should i keep the Magento\LayeredNavigation\Block\Navigation\FilterRenderer file in my custom path...if not if i override the FilterRenderer Block, i have to clone the file as is rite? correct me if im wrong
    – Sushivam
    Commented Oct 18, 2016 at 10:04

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.