Skip to content

filtering by multiselect attribute displaying values in static block

When filtering by text attribute type the module block and phtml template retrieve values by attribute code, however its not not returning values on MultiSelect

ive tried the following:

// Build a custom SQL expression using find_in_set
$findInSetConditions = [];
foreach ($desiredValues as $value) {
$findInSetConditions[] = $productCollection->getConnection()->quoteInto(
‘FIND_IN_SET(?, ‘ . $productCollection->getConnection()->quoteIdentifier(‘e.’ . $attributeCode) . ‘)’,
$value
);
}

and originally – >

$productCollection->addAttributeToFilter(
$attributeCode,
[‘finset’ => $value],
‘left’
);

in the exception error log:

[object] (MagentoFrameworkExceptionLocalizedException(code: 0): Invalid block type: YourVendorYourModuleBlockProductCount

here is the complete code for block
use MagentoCatalogModelResourceModelProductCollectionFactory;
use MagentoFrameworkViewElementTemplate;
use MagentoFrameworkViewElementTemplateContext;

class ProductCount extends Template
{
protected $productCollectionFactory;

public function __construct(
    Context $context,
    CollectionFactory $productCollectionFactory,
    array $data = []
) {
    parent::__construct($context, $data);
    $this->productCollectionFactory = $productCollectionFactory;
}

public function getProductData($attributeCode, $desiredValues)
{
    // Instantiate product collection
    $productCollection = $this->productCollectionFactory->create();

    // Build a custom SQL expression using find_in_set
    $findInSetConditions = [];
    foreach ($desiredValues as $value) {
        $findInSetConditions[] = $productCollection->getConnection()->quoteInto(
            'FIND_IN_SET(?, ' . $productCollection->getConnection()->quoteIdentifier('e.' . $attributeCode) . ')',
            $value
        );
    }

    // Add custom SQL expression to the WHERE clause
    $productCollection->getSelect()->where(implode(' OR ', $findInSetConditions));

    // Add 'name' attribute to the select
    $productCollection->addAttributeToSelect(['name']);

    // Get the count of products with the specified attribute values
    $productCount = $productCollection->getSize();

    // Get the names of products
    $productNames = [];
    foreach ($productCollection as $product) {
        $productNames[] = $product->getName();
    }

    return [
        'count' => $productCount,
        'names' => $productNames,
    ];
}

}

and the template file

getLayout()->createBlock(YourVendorYourModuleBlockProductCount::class);
$attributeCode = ‘camera_type’;
$desiredValues = [‘bullet’];
$productData = $block->getProductData($attributeCode, $desiredValues);
?>

Number of products with = :

0) : ?>

<h2>List of Products:</h2>
<ul>
    <?php foreach ($productData['names'] as $productName) : ?>
        <li><?php echo $productName; ?></li>
    <?php endforeach; ?>
</ul>
<p>No products found with <?php echo $attributeCode; ?> = <?php echo implode(', ', $desiredValues); ?></p>