AgentReadyAI visibility appCaffeine & CommerceShopify agency
Caffeine and Commerce
Caffeine and Commerce
By Dylan HuntAugust 9th, 2019shopifyscripts

Give Returning Customers a Discount with Shopify Scripts

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.

Give Returning Customers a Discount with Shopify Scripts

There are a lot of people asking for more information on how to discount returning customers, so I wrote this script to give a discount based on the number of orders the customer has placed. This script is for people who have placed over 2 orders, and a platinum discount if you've placed over 5, but can be modified for any amount.

customer = Input.cart.customer
discount = 0
message = ""

if customer
  if customer.orders_count > 2
    discount = 1000
    message = "VIP Customer - $10 off"
  end

  if customer.orders_count > 5
    discount = 2000
    message = "Platinum Customer - $20 off"
  end
end

puts discount

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

If you have any other scripts you're wondering about just ask 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.