A Shopify Function
A small piece of code that Shopify runs on its own servers every time the cart changes and again at checkout.
Case study · Shopify · Custom checkout rule
A supplements store sells energy drinks by the block and needed a firm limit: one block per order, whatever the flavour. Shopify has no setting for that, and a limit that lives only in the theme is easy to get around.
01The brief
Energy drinks in the store are sold in blocks, and one unit of a product is one block. The owner wanted every order limited to one block in total, so a customer could not get around the limit by adding one block of each flavour.
A quantity limit built into the theme only hides buttons. The cart can still be changed in other ways, and checkout would accept it. The rule had to be checked by Shopify itself.
02The work
A small piece of code that Shopify runs on its own servers every time the cart changes and again at checkout.
The limit is on energy drinks as a group, so one block of each flavour is still over the limit.
Drinks are recognised by their product category, so a new flavour added to the store is limited from day one, with nothing to remember.
Any other product can be put under the same limit by adding one tag in Shopify admin.
A customer who goes over the limit sees why and what to change, instead of a checkout that just fails.
By design the limit applies to each order. Customers can order again later.
03In detail
Most "limit quantity" solutions change what the theme shows: they hide the plus button or cap the number field. That works until the cart is changed from another page, another device or a direct link. The theme is only the shop window. The order is accepted at checkout.
A Shopify Function sits where the order is accepted. Whatever route the products took into the cart, the rule is checked before payment, and the order cannot be placed while it is broken.
Before the function was switched on in the store, I ran it locally against four saved test carts and checked the answer for each.
At launch the rule covered all 13 energy drinks in the store without listing a single one by hand.
04In numbers
05Need something similar