0

this post gives me a good idea of the basic approach of filters:

Filter product collection by custom attribute

However, products have a custom attribute that is actually going to be stored as a multiselect element (which I assume is treated as an array):

$product->setAttribute('schoolid','11,12,13,18'); //the schoolid attribute for a given product
//or perhaps it's stored as an array
$product->setAttribute('schoolid', array(11,12,13,13));

this needs to be compared against a user's value so a this won't really work:

$collection->addFieldToFilter('schoolid', '13');

as this is more of an array comparison. Is there a standardized way to do this in Magento? Is there any way in other words to convert the product attribute to an array and do an in-array comparison within the existing model?

1
  • explain more whats u wants and why minus rate. Commented Mar 9, 2016 at 9:44

2 Answers 2

0

You can convert your school ids to array and then apply the filter.

$schoolid = '11,12,13,18'; //the schoolid attribute for a given product
$schoolIds = explode(',', $schoolid);

$collection
    //->addFieldToFilter('schoolid', '13');
    ->addFieldToFilter('schoolid', array('in' => $schoolIds));

This should do the trick for you.

UPDATE

If schoolid is a attribute type then filter collection like this:

$collection
    //->addFieldToFilter('schoolid', '13');
    ->addAttributeToFilter('schoolid', array('in' => $schoolIds));
1
  • 1
    This is interesting, why down vote? Commented Mar 9, 2016 at 9:39
-1
addAttributeToFilter() is used to filter EAV collections.

addFieldToFilter() is used to filter Non-EAV collections.

http://fishpig.co.uk/magento/tutorials/addattributetofilter/

$collection->addAttributeToSelect('*');

n - in

$collection->addFieldToFilter('schoolid',array("in"=>array('11', '12', '13','18')))

Not In - nin

$collection->addFieldToFilter('schoolid',array("nin"=>array('11', '12', '13','18')))

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