Magento Forum

How do I use addAttributeToFilter() with a multi-select attribute? 
 
bigcalm
Jr. Member
 
Total Posts:  5
Joined:  2010-04-30
 

My store has a custom attribute which is a multi-select.
I’ve been able to filter products based upon which option has been selected from the multi-select.

$allowedSchools = array(348,350);
$this->_productCollection->addAttributeToFilter('stv_school'$allowedSchools);

Except, this only works when only one option has been selected.
So, the above code will return products which have a stv_school ID of 348 OR 350.

If 2 or more options from the multi-select are chosen, the product(s) aren’t returned in my filter.
So, the above code would not return a product if it was associated with 350 AND 402. Nor does it return a product if it is associated with 348 AND 350.

Is anybody able to shed light on how this can be resolved?

Cheers,
Iain

 
Magento Community Magento Community
Magento Community
Magento Community
 
Denys Babenko
Sr. Member
 
Avatar
Total Posts:  130
Joined:  2010-03-03
Fritz-Elsas-Str. 60
 

$allowedSchools = array(348,350);
$this->_productCollection->addAttributeToFilter('stv_school', array('in'=>$allowedSchools));
 
Magento Community Magento Community
Magento Community
Magento Community
 
bigcalm
Jr. Member
 
Total Posts:  5
Joined:  2010-04-30
 

Thanks Denys Babenko, but this hasn’t changed the results returned.

Your suggestion of using “in” lead me on to looking at “finset”.
The following code does what I required:

$allowedSchools = array(
  array(
    
"finset" => array(348)
  ),
  array(
    
"finset" => array(350)
  ),
);
$this->_productCollection->addAttributeToFilter("stv_school"$allowedSchools);

Cheers,
Iain

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ryan Sun _Kuafu
Guru
 
Total Posts:  317
Joined:  2008-11-14
FL
 
Denys Babenko - 05 August 2010 06:14 AM

$allowedSchools = array(348,350);
$this->_productCollection->addAttributeToFilter('stv_school', array('in'=>$allowedSchools));

doubt if it will work, multi-select are stored as comma separated value in eav entity,
maybe you have to

$condArr = array();
foreach(
$allowedSchools as $school)
{
  $condArr[] 
= array($school);
  
$condArr[] = array('like' =>$school',%';
  
$condArr[] = array('like' => '%,' $school;
  
$condArr[] = array('like' => '%,' $school ',%';
}
$this
->_productCollection->addAttributeToFilter('stv_school'$condArr);
 
Magento Community Magento Community
Magento Community
Magento Community
 
Ryan Sun _Kuafu
Guru
 
Total Posts:  317
Joined:  2008-11-14
FL
 

btw ‘finset’ is a string search, 348 would match a string like ‘11348, 529’ which is not what you want…

 
Magento Community Magento Community
Magento Community
Magento Community
 
bigcalm
Jr. Member
 
Total Posts:  5
Joined:  2010-04-30
 

Hi Ryan, thanks for you thoughts.

I tested FIND_IN_SET in MySQL to see what I would get:

mysqlSELECT FIND_IN_SET("348""11348,529");
+---------------------------------+
FIND_IN_SET("348""11348,529") |
+---------------------------------+
|                               
|
+---------------------------------+
1 row in set (0.00 sec)

mysqlSELECT FIND_IN_SET("348""11,3481,529");
+-----------------------------------+
FIND_IN_SET("348""11,3481,529") |
+-----------------------------------+
|                                 
|
+-----------------------------------+
1 row in set (0.00 sec)

mysqlSELECT FIND_IN_SET("348""11,348,529");
+----------------------------------+
FIND_IN_SET("348""11,348,529") |
+----------------------------------+
|                                
|
+----------------------------------+
1 row in set (0.00 sec)

This seems to show that it only does whole matches.

The code that I pasted has so far done exactly as required without any extra products showing that shouldn’t.

I’m glad that I’ve been able to bounce thoughts around in this forum today!

Cheers,
Iain

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ryan Sun _Kuafu
Guru
 
Total Posts:  317
Joined:  2008-11-14
FL
 

great, thanks for the test

 
Magento Community Magento Community
Magento Community
Magento Community
 
Track 23
Member
 
Total Posts:  50
Joined:  2008-03-19
 

Nice. That finset code worked for me. Mine’s a little more condensed since I only have one value I’m looking for. Very useful when attributes could have multiple values.

$requested_location = array("finset"=>$_GET['location']);
$products->addAttributeToFilter('event_location',$requested_location);

Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
luisa
Jr. Member
 
Avatar
Total Posts:  14
Joined:  2008-04-03
 
bigcalm - 05 August 2010 06:37 AM


$allowedSchools = array(
  array(
    
"finset" => array(348)
  ),
  array(
    
"finset" => array(350)
  ),
);
$this->_productCollection->addAttributeToFilter("stv_school"$allowedSchools);

This worked for me. My filter is working and the performance is still good. Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
sunilpatil
Jr. Member
 
Total Posts:  11
Joined:  2008-11-21
 
Ryan Sun _Kuafu - 05 August 2010 06:46 AM

Denys Babenko - 05 August 2010 06:14 AM
$allowedSchools = array(348,350);
$this->_productCollection->addAttributeToFilter('stv_school', array('in'=>$allowedSchools));

doubt if it will work, multi-select are stored as comma separated value in eav entity,
maybe you have to

$condArr = array();
foreach(
$allowedSchools as $school)
{
  $condArr[] 
= array($school);
  
$condArr[] = array('like' =>$school',%';
  
$condArr[] = array('like' => '%,' $school;
  
$condArr[] = array('like' => '%,' $school ',%';
}
$this
->_productCollection->addAttributeToFilter('stv_school'$condArr);

thanks!!! it works for me.

 
Magento Community Magento Community
Magento Community
Magento Community
 
ravinder_rvtech
Jr. Member
 
Total Posts:  4
Joined:  2012-09-04
 

Thanks “Ryan Sun _Kuafu” your solution is working perfectly.. you saved my day.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ricardo_Martins
Member
 
Avatar
Total Posts:  48
Joined:  2009-02-04
Santos, SP, Brazil
 
Ryan Sun _Kuafu - 05 August 2010 06:46 AM

Denys Babenko - 05 August 2010 06:14 AM
$allowedSchools = array(348,350);
$this->_productCollection->addAttributeToFilter('stv_school', array('in'=>$allowedSchools));

doubt if it will work, multi-select are stored as comma separated value in eav entity,
maybe you have to

$condArr = array();
foreach(
$allowedSchools as $school)
{
  $condArr[] 
= array($school);
  
$condArr[] = array('like' =>$school',%';
  
$condArr[] = array('like' => '%,' $school;
  
$condArr[] = array('like' => '%,' $school ',%';
}
$this
->_productCollection->addAttributeToFilter('stv_school'$condArr);

It works for me. Just need to close arrays.. =)

$condArr[] = array($school);
  
$condArr[] = array('like' =>$school',%');
  
$condArr[] = array('like' => '%,' $school);
  
$condArr[] = array('like' => '%,' $school ',%');
 
Magento Community Magento Community
Magento Community
Magento Community
 
ukashkartim
Jr. Member
 
Total Posts:  3
Joined:  2014-05-02
 

yes Ricardo_Martins

i resolved thanks for your helpfull response

_____________________________________________________________________________________________________________
UKASH - UKASH

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top