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?
2 Answers 2
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 thisND17– ND172015年10月30日 13:12:33 +00:00Commented Oct 30, 2015 at 13:12
-
I want to filter start_date and end_date, How do you do?MrTo-Kane– MrTo-Kane2016年03月24日 08:45:39 +00:00Commented Mar 24, 2016 at 8:45
-
where should I place this code??zekia– zekia2017年11月21日 08:02:38 +00:00Commented Nov 21, 2017 at 8:02
-
@ktsixit Where you need it.Marius– Marius2017年11月21日 08:15:43 +00:00Commented 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?jack– jack2018年12月25日 15:37:14 +00:00Commented Dec 25, 2018 at 15:37
This code will combine two finset conditions with OR:
->addAttributeToFilter(
array(
array('attribute'=>'some_attribute', 'finset'=>$v1),
array('attribute'=>'some_attribute', 'finset'=>$v2),
)
);
catalog_product_index_eav_idxtable.