2

I have a multi-select product attribute and i am wanting to exclude several values from a collection.

i.e

attribute = filter_theme
possible values = A, B, C, D, E, F, G, H

If i use nin like so:

$collection->addAttributeToFilter('filter_theme', array('nin' => $exclude));

It will only work if the products only have a single selection on filter_theme i.e value A

If they have more than one it won't work i.e values A & B. (I know this is also the case when using in to find rather than exclude)

Again i know that i can use finset to find any products with $exclude, whether it has 1 or all options selected:

$collection->addAttributeToFilter('filter_theme', array('finset' => $exclude));

This will work on both value A only and value A & B & Cbeing selected.

Is there a way of doing the opposite of finset to exclude rather than include so that it works no matter how many values are selected for my attribute?

1

What about using finset and manipulating your array instead?

You could do the following:

$possibleValues = array('A','B','C'...);
$excludeValues = array('B', 'C'...);

$includeValues = array_diff($possibleValues, $excludeValues);

And then you can just use your finset on includeValues

| improve this answer | |

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.