Magento 2.1 – Product Count in Layer Navigation for Text Swatches

layered-navigationmagento-2.1productswatches

I want to show the product count on layer navigation for text swatches.

By default Magento providing this feature for normal attributes(Text, Dropdown, and multi-select) in layer navigation but is not available for swatches.

Please see the below-attached screenshot for more clarity.

I Want like below

enter image description here

Magento by default providing for another attribute like text, dropdown

enter image description here

Please help on above issue.Any references or suggestions highly appreciated

Thanks

Best Answer

Look at \Magento\Swatches\Block\LayeredNavigation\RenderLayered::getOptionViewData

You can modify option returning option data to

return [
    'label' => $swatchOption->getLabel(),
    'link' => $linkToOption,
    'custom_style' => $customStyle,
    'count' => $filterItem->getCount()
];

and render product count in Magento/Swatches/view/frontend/templates/product/layered/renderer.phtml

<?php foreach ($swatchData['options'] as $option => $label): ?>
    <?= $label['count']; ?>

Note, that you must not modify core module code. Instead that, you can use base ways to customizing module behavior (plugins, layouts, etc)

Related Topic