Follow below steps to display configurable product specific attribute values count on product listing page in Magento 2.
First, you need to override vendor/magento/module-catalog/Block/Product/ListProduct.php file in your existing module.
To override this file:
Create new file ‘di.xml’ at this path -> app/code/Vender/Modulename/etc/. Add the following code in this new file:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="MagentoCatalogBlockProductListProduct" type="VenderModulenameBlockProductListProduct" /> </config>
Then create a new file named ‘ListProduct.php’ at app/code/Vender/Modulename/Block/Product/.
Add the following code in this new file:
<?php
namespace VenderModulenameBlockProduct;
class ListProduct extends MagentoCatalogBlockProductListProduct {
public function getProductDetailsHtml(MagentoCatalogModelProduct $product) {
$html = '';
$renderer = $this->getDetailsRenderer($product->getTypeId());
if ($renderer) {
if ($product->getTypeId() == MagentoConfigurableProductModelProductTypeConfigurable::TYPE_CODE) {
$options = array();
$productColor = array();
$data = $product->getTypeInstance()->getConfigurableOptions($product);
foreach ($data as $attr) {
foreach ($attr as $p) {
if ($p['attribute_code'] == 'color') {
if (!in_array($p['option_title'], $productColor)) {
$productColor[] = $p['option_title'];
}
}
}
}
$html = '<p> ' . count($productColor) . ' </p>';
}
$renderer->setProduct($product);
return $html . $renderer->toHtml();
}
return '';
}
}
?>

