Shopify Theme Reviews

Shopify Checkout Validation Function: Block Bad Carts with Clear Errors (2027)

Fayyaz Ahmed

Shopify checkout validation with your own Cart and Checkout Validation Function: limit a product to 3 per order, block checkout under a $25 minimum, and refuse a PO Box address for heavy items. Episode 8 of the Shopify Functions 2027 series (API 2027-01). Real store tests: with the rule off, 4 beanies go into the cart. With it on, the cart stays at 3 with our message, a $22 checkout shows "Minimum order is 25.00 USD" when the buyer tries to complete it, and a PO Box address for a snowboard gets an error on the address field.

Chapters

  • 0:00 Max 3 per order: the cart refuses a 4th beanie
  • 0:35 Before: the validation is off
  • 0:51 What a cart and checkout validation can do
  • 1:32 The buyer journey: cart, checkout, Complete order
  • 1:58 Generate the Function (API 2027-01)
  • 2:25 The input query
  • 3:06 Rules in a JSON metafield
  • 3:30 Rule 1: limit quantity per order
  • 3:58 Rule 2: minimum order amount, checkout only
  • 4:21 Rule 3: no PO Box for heavy items
  • 4:44 Switch it on: scope + validationCreate
  • 5:26 Real store: cart limit + PO Box address error
  • 6:19 Minimum order at Complete order
  • 7:00 The real logs: 250K-360K instructions
  • 7:25 Real runs become tests
  • 7:53 Gotchas: errors always block, per order not per customer
  • 8:46 Recap

Step by step (full walkthrough)

Max 3 per order: the cart refuses a 4th beanie

Three beanies in the cart. $66. The buyer presses plus once more, and the cart stays at three, with our message right under the line.

This is episode eight of Shopify Functions 2027: Shopify checkout validation, with a Cart and Checkout Validation Function. We limit a product to three per order. We block checkout under a twenty-five dollar minimum.

We refuse a PO Box address when the cart has a heavy item. And I show you the real logs, the tests, and the gotchas I hit on a real store.

Before: the validation is off

First, the store without our Function. I switched the validation off. The Merino Wool Beanie, $22, quantity four.

Add to cart. Four beanies, $88. Nothing stops it.

What a cart and checkout validation can do

A validation Function runs on the cart and in checkout. The docs also list accelerated checkouts, B2B and draft orders. It returns errors.

Each error has a message for the buyer and a target, which says where the message goes. And an error always blocks. There is no warning mode.

If your Function returns an error, the buyer can't continue until the cart is fixed. A store can activate up to twenty-five validation Functions. The docs list Point of Sale, order edits and subscription renewals as not supported.

So this is for online carts and checkouts.

The buyer journey: cart, checkout, Complete order

The input tells you where the buyer is, in a field called buyer journey step. Cart interaction means the buyer is adding or changing a line in the cart. Checkout interaction means the buyer changed something in checkout, like the address.

And checkout completion means the buyer pressed Complete order. I saw all three in my logs. So one Function can decide which rule belongs to which step.

Generate the Function (API 2027-01)

shopify app generate extension, template cart checkout validation, flavor vanilla JS. The template writes 2026, oh seven, so we pin 2027, oh one, then run function schema and typegen. The handle is checkout rules.

We need it to switch the validation on. One target: cart, validations, generate, run. One target, one export.

That's the whole config.

The input query

The input query. First, the buyer journey step. The cart subtotal, with its currency.

Each line: the quantity, the product title, and hasTags. We ask for two tags, limit three and heavy, and Shopify answers true or false for each one. The delivery address, the two street lines, so we can spot a PO Box.

The currency rate, so our minimum works in any currency. And a metafield on the validation itself. That's where our rules live.

The tags decide which products a rule applies to. The numbers come from the metafield.

Rules in a JSON metafield

Here are the rules, stored as JSON on the validation. Three of each product tagged limit three. A twenty-five dollar minimum subtotal.

And no PO Box address when a heavy product is in the cart. Because the rules are data, a merchant or your admin page can change them without deploying the Function again.

Rule 1: limit quantity per order

Rule one. Read the rules and the step, and start an empty list of errors. For every line with the limit three tag, if the quantity is over the maximum, add an error.

The message names the product and says how many to remove. The target is dollar, dot, cart: the whole cart. This rule runs at every step, so the cart already refuses the fourth beanie.

Rule 2: minimum order amount, checkout only

Rule two. The minimum, converted with the currency rate, and the subtotal. Notice the step check.

Errors block, so if this rule also ran on the cart, a buyer could never add the first twenty-two dollar beanie. The cart would refuse it. So the minimum only applies in checkout.

The message says how much more to add.

Rule 3: no PO Box for heavy items

Rule three only runs when the cart has a product tagged heavy. For each delivery group, check both street lines for a PO Box. This time the target is the first address line of that delivery group.

Checkout shows the message right under that field. Then we return one validation add operation with all the errors. An empty list means the cart is fine.

Switch it on: scope + validationCreate

To switch it on, the app needs the write validations scope. Add it to the app config and restart app dev. Then validation create, with the function handle, a title, enable true, and the rules JSON in the same call.

Block on failure decides what happens if the Function itself crashes or times out. False means checkout continues without the validation. True means checkout is blocked.

Errors you return always block either way. The merchant sees it in Settings, Checkout. Under Checkout rules: Checkout rules, episode eight, by our app, active.

The merchant can switch it off here.

Real store: cart limit + PO Box address error

Now the real store, with the validation on. Three beanies in the cart. Plus, one more.

The cart stays at three, and shows our message: Merino Wool Beanie, limit three per order. Remove one to continue. At the cart step, Shopify refuses the change itself.

The log shows the Function received four beanies and returned the error, so the fourth never went in. Next, the Red Snowboard, $100, tagged heavy, straight to checkout. The buyer types PO Box one two three four.

A moment later, the address field turns red: heavy items can't ship to a PO Box. Please use a street address. The buyer changes it to a street address.

And the error is gone. Checkout runs the Function on every address change.

Minimum order at Complete order

Last, one beanie: $22, under our minimum. While the buyer fills in checkout, the Function already returns the error, but Shopify doesn't show a whole-cart error yet. When the buyer presses Complete order, it appears at the top: minimum order is twenty-five U S D.

Add three U S D more to check out. I pressed it once, in a test with a manual payment method. The log shows checkout completion with our error, and no order was created.

Back in that same checkout, the banner is still at the top. It stays there while the cart is under $25. Add $3, and it goes away.

The real logs: 250K-360K instructions

These are real runs from the app dev logs. The empty cart, three beanies, then four, which returned the limit error. In checkout, the twenty-two dollar cart returned the minimum order error on every change, and again at completion.

The heaviest run, the snowboard to a PO Box, used about 360,000 instructions, out of the eleven million a Function may use.

Real runs become tests

Five real runs went into the repo as test fixtures: three beanies, four beanies, the twenty-two dollar completion, a snowboard before the address, and a snowboard to a PO Box. On every push, GitHub Actions builds the Function, runs it with the PO Box input, and checks all five fixtures. All five passed, with the same instruction count as on the store.

So if a later change lets a fourth beanie through, the test fails before any buyer sees it.

Gotchas: errors always block, per order not per customer

Gotcha one. Errors always block. A bug can stop every checkout in the store, so start with one tagged product, and test the cart and checkout before you widen the rule.

Gotcha two. An error on dollar, dot, cart appears when the buyer presses Complete order. If you want the buyer to see it earlier, target a field, like we did with the address.

Gotcha three: if the Function crashes or times out, checkout continues without it, unless you set block on failure to true. Gotcha four: a validation only sees this cart. It can't count what a customer bought last week, so limit three per customer is really limit three per order.

And five. Point of Sale, order edits and subscription renewals are not checked by it, so don't rely on it there.

Recap

Let's recap. A validation Function runs on the cart and in checkout. Read the buyer journey step and decide which rule belongs to which step.

Return errors with a message and a target: the whole cart, or one field. Our rules live in the validation's own metafield, so they change without a redeploy. To switch it on: the write validations scope, then validation create, with the function handle.

The code is on GitHub, tag E P zero eight, link in the description. Next episode: fulfillment constraints, choosing which location ships what. See you there.

The Shorts from this video

Shopify Limit Quantity per Order: Max 3 with a Checkout Rule

Shopify limit quantity per order with a Cart and Checkout Validation Function: products tagged limit-3 can't go above 3 in one cart. Real store: before the rule 4 beanies go in, after it the cart stays at 3 with our message. Note: a validation sees one cart, so it's per order, not per customer. API 2027-01.

Shopify Minimum Order Amount: Block Checkout Under $25

Shopify minimum order amount with a Cart and Checkout Validation Function: under $25 the buyer can't complete the order and checkout says "Minimum order is 25.00 USD. Add 3.00 USD more to check out." The rule only runs in checkout, so the cart can still be filled. API 2027-01.

Shopify Checkout Address Validation: No PO Box for Heavy Items

Shopify checkout address validation with a Cart and Checkout Validation Function: when the cart has a product tagged heavy, a PO Box address gets an error right on the address field. A street address clears it. The error target is $.cart.deliveryGroups[0].deliveryAddress.address1. API 2027-01.

🔔 New episodes every week on the YouTube channel. Need a custom Shopify app or API integration? Wixob Technologies builds them: info@wixob.com.