AgentReadyAI visibility appCaffeine & CommerceShopify agency
Caffeine and Commerce
Caffeine and Commerce
By Dylan HuntDecember 9th, 2018shopifyscripts

How to Give an Automatic Discount on Shopify Based on Cart Value

Shopify Scripts is being retired on June 30, 2026.

The Ruby Scripts and Script Editor used in this tutorial stop running after that date. The replacement is Shopify Functions (also called checkout functions), which is faster, available beyond Shopify Plus, and built for the new checkout. If you run Scripts today, plan your migration now.

How to Give an Automatic Discount on Shopify Based on Cart Value

One of the biggest issues I have seen with scripts is that it is really easy to give discounts based on line item value, but calculating the cart value, and applying logic based on that is causing a lot of people issues. Here is a REALLY easy way to solve for this and I will give two examples.

1. $X Off if Cart is over $Y

This will give the customer a discount of $5 if the cart is over $30.

min_discount_order_amount = Money.new(cents:100) * 30

total = Input.cart.subtotal_price_was

discount = if total > min_discount_order_amount
  500
else
  0
end

message = "My message"

Input.cart.line_items.each do |line_item|
  product = line_item.variant.product
  next if product.gift_card?
  line_item.change_line_price(line_item.line_price - Money.new(cents: discount), message: message) unless discount == 0
end

Output.cart = Input.cart

2. Variable discount depending on cart value

This will give the customer a discount of $5 if the cart is over $50 OR will give them $10 off if the cart is over $100.

min_discount_order_amount = Money.new(cents:100) * 50
min_discount_order_amount_2 = Money.new(cents:100) * 100

total = Input.cart.subtotal_price_was

discount = if total > min_discount_order_amount_2
  1000
elsif total > min_discount_order_amount
  500
else
  0
end

message = "My message"

Input.cart.line_items.each do |line_item|
  product = line_item.variant.product
  next if product.gift_card?
  line_item.change_line_price(line_item.line_price - Money.new(cents: discount), message: message) unless discount == 0
end

Output.cart = Input.cart

As always, let me know if there are any questions in the comments.

Make your store agent-ready

Get found and recommended by AI shopping assistants.

AgentReady adds Schema.org structured data, an llms.txt directory, and an AI-readability audit to your Shopify store, so ChatGPT, Perplexity, and Google can understand and recommend your products. Free for stores under 500 products.

Comments

Every comment here comes from a verified email. Write yours, confirm from your inbox, and it's live.

Loading comments…

Leave a comment

ShareXLinkedInFacebook

Written by Dylan Hunt, Founder, Caffeine and Commerce. We build Shopify stores that rank and that AI agents can read. Have a project? Get in touch.