4

I want to filter multiselect product attribute which has backend type Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend. So when I save attribute it gets saved as the comma-separated values in the database. So at the time of Product filtering, I applied filter by 2 values as like this

 $this->collection->addAttributeToSelect('*');
 $this->collection->addAttributeToFilter( 'concerns', array('finset' => 'concerns_1,concerns_4'));

It's not filtering with the above code (means collection size returning zero) but when I filter on a single value its working fine. Please suggest me how can I filter on multiple values. Thanks in advance.

3
  • check this link --- magento.stackexchange.com/a/14116/85907 Jun 23, 2020 at 4:54
  • I tried that but it's not working for me
    – AjayKr
    Jun 23, 2020 at 4:55
  • I think one problem with FIND_IN_SET is that it does not check for multiple values at one time. suppose I'm writing SELECT FIND_IN_SET('a,b','a,b,c,d'); its returning 0 however both a & b are in the set , but when I write SELECT FIND_IN_SET('b','a,b,c,d'); its returning 2.
    – AjayKr
    Jun 23, 2020 at 4:59

1 Answer 1

3

Need to pass to the addAttributeToFilter and array of arrays.

->addAttributeToFilter(
    array(
          array('attribute'=>'concerns', 'finset'=>'a'),
          array('attribute'=>'concerns', 'finset'=>'b'),
    )
);

Updated :-

->addAttributeToFilter('concerns', array('in' => $array));
6
  • I have tried this but it's not working. My question was suppose one product has attribute values a,b,c,d and I want to filter on attribute a,d then how can I do that? I tried your as you have provided here but it's not working @rkrathod
    – AjayKr
    Jun 23, 2020 at 6:25
  • check my updated answer Jun 23, 2020 at 6:25
  • Can we do it in one line? is it possible? because my attribute values are dynamic some time it might have 3 values some time might have 4 value etc. can we handle that type of scenario?
    – AjayKr
    Jun 23, 2020 at 6:28
  • i think it possible using 'in' condition Jun 23, 2020 at 6:32
  • 2
    I tried updated one but it's not working
    – AjayKr
    Jun 23, 2020 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.