8

That's not a typo.

I am aware that I need to use 'finset' to filter my multi-select attributes; however, I am trying to filter multiple values at once and getting:

Incorrect parameter count in the call to native function 'FIND_IN_SET.

Here's some sample code:

foreach ($options as $option) {
 // $option[0] contains an attribute ID as a string
 $attribute = Mage::getModel('catalog/resource_eav_attribute')->load($option[0]);
 if ($attribute->getFrontendInput() == 'multiselect') {
 $collection->addAttributeToFilter($attribute->getAttributeCode(), array('finset' => $option[1]));
 } else {
 $collection->addAttributeToFilter($attribute->getAttributeCode(), array('in' => $option[1]));
 }
}

What I have on the frontend is a set of fields, each set corresponds to a specific attribute and contains checkboxes for each attribute value. Based on these submissions, the collection should filter out what has been selected.

Everything works great except for the single case where I attempt to filter two of the multi-select options at the same time. If I only choose one of them, the search works properly. If I choose two or more, I get the MySQL error mentioned above.

Any ideas? Or am I forced to use custom SQL statements to build this filter?

asked Jan 30, 2014 at 3:07
1
  • If you are trying to filter multiple select input type try filtering using catalog_product_index_eav_idx table. Commented Mar 1, 2014 at 8:30

2 Answers 2

22

From what I understand you want to send 2 or more values and filter by them using OR.
Something like this:

...
WHERE
 FIND_IN_SET($v1, `some_field`) OR
 FIND_IN_SET($v2, `some_field`) OR
...

$v1 & $v2 are the values of your checkboxes

For that you need to pass to the addAttributeToFilter and array of arrays.

->addAttributeToFilter(
 array(
 array('attribute'=>'some_attribute', 'finset'=>$v1),
 array('attribute'=>'some_attribute', 'finset'=>$v2),
 )
);

Here is a possible implementation.

$data = THE ARRAY OF THE CHECKBOX VALUES;
$filter = array();
foreach ($data as $value) {
 $filter[] = array(
 'attribute' => $attribute->getAttributeCode(),
 'finset' => $value
 );
}
if (count($filter) > 0) {
 $collection->addAttributeToFilter($filter);
}
answered Jan 30, 2014 at 7:18
6
  • I am getting my category id like this($exist_prdcat_id = 4,5 so i use ->addAttributeToFilter('category_id', array( array('finset' => array($exist_prdcat_id)), )); but i am not getting product why?? can you tell me this Commented Oct 30, 2015 at 13:12
  • I want to filter start_date and end_date, How do you do? Commented Mar 24, 2016 at 8:45
  • where should I place this code?? Commented Nov 21, 2017 at 8:02
  • @ktsixit Where you need it. Commented Nov 21, 2017 at 8:15
  • How can i perform 'and'. I mean want to filter product collection based on multiple multi-select attribute values. @Marius can you help? Commented Dec 25, 2018 at 15:37
-1

This code will combine two finset conditions with OR:

->addAttributeToFilter(
 array(
 array('attribute'=>'some_attribute', 'finset'=>$v1),
 array('attribute'=>'some_attribute', 'finset'=>$v2),
 )
);
Fabian Schmengler
66.2k25 gold badges191 silver badges422 bronze badges
answered Jul 8, 2015 at 11:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.