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?

  • If you are trying to filter multiple select input type try filtering using catalog_product_index_eav_idx table. – Subesh Pokhrel Mar 1 '14 at 8:30
17

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);
}
  • 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 – ND17 Oct 30 '15 at 13:12
  • I want to filter start_date and end_date, How do you do? – MrTo-Kane Mar 24 '16 at 8:45
  • where should I place this code?? – zekia Nov 21 '17 at 8:02
  • @ktsixit Where you need it. – Marius Nov 21 '17 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? – jack Dec 25 '18 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),
    )
);

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.