0

I have gone through the Search Criteria functionality to filter the products, for this I have created the custom attributesmart_search multiselect attribute. And I have written some code for filter those items.

But it is not working for me, for this I written below stuff.

<?php

namespace ABCCompany\CustomerSearch\Observer;

class RedirectCustomerSearch implements \Magento\Framework\Event\ObserverInterface{



    /**
    * @var \Magento\Catalog\Model\ProductRepository
    */
    protected $_productRepository;


    /**
    * @var \Magento\Framework\Api\SearchCriteriaBuilder
    */
    protected $_searchCriteria;


    public function __construct
    (
        \Magento\Catalog\Model\ProductRepository $productRepository,
        \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
    ){
        $this->_productRepository = $productRepository;
        $this->_searchCriteria = $searchCriteriaBuilder;
    }

    public function getSmartSearch($search){

        $searchCriteria = $this->_searchCriteria->addFilter('smart_search', $search)->create();
        $result = $this->_productRepository->getList($searchCriteria);
        return $result->getItems() 

    }

}

In smart_search options have blue color, binding, Comics etc.. I have passed blue color, but it is returning nothing.

Could you please suggest me where I went wrong?

1 Answer 1

1

As your custom attribute 'smart_search' is a multiselect attribute, on the creation of the search criteria the value that you have to pass is the option value and not the option label like blue color. Additionally, the default condition type of the addFilter function in 'eq'. As your custom attribute is multiselect you have to change this to 'finset'.

Try something like below, where $search is the option value of your custom attribute.

$searchCriteria = $this->_searchCriteria->addFilter('smart_search', $search, 'finset')->create();

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.