0

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] (Magento\Framework\Exception\LocalizedException(code: 0): Invalid block type: YourVendor\YourModule\Block\ProductCount

here is the complete code for block use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; use Magento\Framework\View\Element\Template; use Magento\Framework\View\Element\Template\Context;

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(\YourVendor\YourModule\Block\ProductCount::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>

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.