Shopify smart collections, for Magento
I've built a Magento 2 module that fills categories from a rule instead of by hand. Describe the products you want, save, and the category keeps itself up to date as your catalogue changes.
In Shopify you can create a collection, give it a condition such as "price is greater than 100" or "tag is equal to summer", and never think about it again. Products join the collection when they match and leave when they stop matching. Shopify calls these smart collections, and merchants who move from Shopify to Magento ask where they went.
In Magento Open Source they never existed. Category membership is a grid of checkboxes. A "Under $50" category is correct on the day somebody builds it and wrong after the next price change. A "New arrivals" or "Blue" category depends on someone remembering to tick the box for every new product. So I built a module that does what Shopify does.
What it does
The module adds a Smart Rules section to the category form in the admin. Switch it on, build a rule, and the category's products are decided by that rule from then on.
- Products are assigned when they start to match the rule and removed when they stop.
- Conditions combine with ALL or ANY, the same two options Shopify gives you.
- Saving a product or category queues the work, so saves in the admin stay fast.
- A full check of every smart category runs each night, which catches changes made by imports and bulk updates.
- A console command recomputes one category, or all of them, on demand.
Categories that are not flagged as smart are never touched, so you can turn it on for one category and leave the rest of the tree alone.
Setting up a rule
- 01Go to Catalog > Categories and open a category.
- 02Open Smart Rules and switch "Enable smart rules" on.
- 03Click +, choose an attribute, then set the operator and value. Use ALL or ANY to combine several conditions.
- 04Save.
The condition builder is the one Magento already uses for the Products List widget, so anyone who has configured that widget already knows how to use it. With cron running, the category updates once the queue has been processed. To apply a rule straight away:
# One category
bin/magento upturnstudio:smart-categories:recompute --category-id=42
# Every smart category
bin/magento upturnstudio:smart-categories:recomputeHow it works
There is no custom rule engine. The module hands the stored conditions to the same code the Products List widget uses, which turns them into SQL filters on a product collection. Matching happens in the database in one query, not by loading each product and testing it in PHP, so it holds up on large catalogues.
The result is compared with the products currently in the category, and the difference is written to catalog_category_product, the same table the checkbox grid writes to. That was the design decision I cared most about. As far as the rest of Magento is concerned, a smart category is an ordinary category with ordinary product assignments, so layered navigation, sorting, indexers and your theme all work without knowing the module exists.
Saves go through the message queue. Saving a category queues a recompute of that category. Saving a product queues a check of that one product against every smart category. If the store has no smart categories, the product save observer does nothing at all. The nightly run at 3am is the safety net for anything that changes products without firing a save event, and the schedule is configurable.
Getting it
The module is available from my modules store, and you get access and install instructions straight after checkout. It needs Magento 2 with Magento_PageBuilder enabled and cron running. Settings are under Stores > Configuration > UpturnStudio > Smart Categories, where you will find a master switch and the cron schedule for the nightly check.
What it does not do yet
These are the limitations I know about. Check them against your catalogue before you rely on the module.
- ANY rules can miss products. A product with no value for one of the attributes in an ANY rule is left out, even when it matches another condition. ALL rules are not affected.
- Hidden and disabled products are assigned. Products set to "Not Visible Individually" can match a rule, so the product count in the admin can be higher than on the storefront.
- Rules use default store values only. Store view overrides are ignored.
- The storefront can lag behind the admin. With indexers set to "Update by Schedule", the storefront changes on the next indexer cron run.
The short version
- Smart Categories brings Shopify-style smart collections to Magento 2: a category filled by a rule, not by hand.
- Rules are built in the category form with the condition builder from the Products List widget.
- Updates run through the message queue on save, with a nightly full check and a console command for on-demand recomputes.
- Assignments are written to the standard category product table, so the rest of Magento treats a smart category like any other.
- It is available now from my modules store.
If you'd like a hand installing it, or you have a category structure that needs rules the module doesn't cover yet, get in touch.