1

In Magento 2.4.1 I have a custom multiple select attribute 'theme' The theme attribute contains options 'football' and 'sport' Some products have both attribute options applied. I can apply both attribute values to a product fine but when I filter in product grid dashboard, it does not find products which have more than one attribute value applied. If a product has one attribute value only then it filters correctly but if a product has multiple attribute filters applied it does not show. Is this a default Magento 2 setting? Thank you.

1 Answer 1

0

I did take a look at this question. A multiselect attribute gets stored in a table with varchar suffix. For instance, product attribute activity (present in Magento sample data) is stored in catalog_product_entity_varchar. Also, the value is a list of options ids.

see attachedenter image description here screenshot.

Therefore, your query has to use a like filter, see below an example.

<?php

namespace Mbs\MultiSelectFilter\Command;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class GetProductWithSeveralMultiselectOptions extends Command
{
    /**
     * @var ProductRepositoryInterface
     */
    private $productRepository;
    /**
     * @var SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;
    /**
     * @var FilterBuilder
     */
    private $filterBuilder;

    /**
     * GetProductWithSeveralMultiselectOptions constructor.
     * @param ProductRepositoryInterface $productRepository
     * @param SearchCriteriaBuilder $searchCriteriaBuilder
     * @param FilterBuilder $filterBuilder
     * @param string|null $name
     */
    public function __construct(
        ProductRepositoryInterface $productRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        FilterBuilder $filterBuilder,
        string $name = null
    ) {
        parent::__construct($name);
        $this->productRepository = $productRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->filterBuilder = $filterBuilder;
    }

    protected function configure()
    {
        $this->setName('mbs:find:product');
        $this->setDescription('Find Product in catalog focusing with multiselect attribute');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $filter = $this->filterBuilder->setField('activity')
            ->setConditionType('like')
            ->setValue('%5432%')
            ->create();

        $searchCriteria = $this->searchCriteriaBuilder->addFilters([$filter])->create();

        $result = $this->productRepository->getList($searchCriteria);

        foreach ($result->getItems() as $product) {
            $output->writeln(sprintf('sku: %s', $product->getSku()));
        }

        $output->writeln('CFart log import complete');
    }
}
2
  • Thank you for the response. I'm fairly new to Magento, how would I use this and where? Commented Nov 14, 2020 at 18:40
  • This is a command-line script showing you how to build a query with a multiselect attribute. I read that is what you needed. Good luck with Magento Commented Nov 14, 2020 at 20:55

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.