This week, “D.” in Richmond, Surrey, writes:

I’m trying to setup a filter so that my feed contains ALL items whose brand is “Acme,” and items from other brands ONLY if their price is £5 or more.

What Is A Filter, Anyway?
We talked about filters a few posts back when we talked about Boolean functions. To quote myself:

[A filter is] a business rule that outputs “True” or “False.” You can think of it like this: the filter is asking “Should I exclude the item?” If the rule returns “True”, the item is excluded; if it returns false, the item is not excluded.

What’s tricky is that D. has expressed his filter backwards: he’s described the items he wants to include. To properly write the filter rule, we have to turn that into a description of the items he wants to exclude. So in the spirit of Bizarro-land, let’s start from the end of his description.

He wants to keep items “ONLY if their price is £5 or more.” That means he wants to exclude items if their price is less than £5. A filter that would do that is simply: $currentprice < 5. But wait: he wants to include items if their brand is “Acme,” regardless of the price, so the exclude rule is really that the price is less than £5 AND the brand is not Acme. That would look like this:

$currentprice < 5 AND NOT($brand = "Acme")

Ok, I’ll admit sometimes it’s not always obvious how to write these rules, so here are some rules of thumb:

  • Always try to think in terms of items you don’t want in the feed.
  • You can apply multiple filters to a template, so always try to think of the simplest rule that will throw out some of the items you don’t want. For example, if you want to filter out ALL items that are less than £5, and you also want to filter out ALL items that have the word “blue” in the title, make a separate filter for each of those conditions and apply them both to your template. Note: this is different from filtering out items that are less than £5 AND have the word “blue” in the title.
  • If, like D., you want to exclude items based on one criterion (e.g., price), except for the ones meeting another criterion (e.g., brand), use the “AND/NOT” pattern I described here.

Good luck, and send me your questions!