Example scripts

Scripts are flexible and can be used to create unique and powerful discounts. For example, you can create scripts that change the prices and properties of line items in your online store's cart based on the items that a customer adds to it.

Shopify Plus

Scripts and the Script Editor app are available to Shopify Plus merchants only.

You can find examples of common scripts in the script templates that are provided in the Script Editor. To view these examples, you need to create a script.

Here are some other script examples:

Percentage off all items

This line item script multiplies the price of each line item in the cart by 0.9, resulting in a 10% discount.

Input.cart.line_items.each do |item|
  item.change_line_price(item.line_price * 0.9, message: "10% off all items!")
end

Output.cart = Input.cart

Percentage off all shipping rates

This shipping script discounts all shipping rates by 0.10, resulting in a 10% discount on shipping.

Input.shipping_rates.each do |shipping_rate|
  next unless shipping_rate.source == "shopify"
  shipping_rate.apply_discount(shipping_rate.price * 0.10, message: "Discounted shipping")
end

Output.shipping_rates = Input.shipping_rates

Remove a shipping rate

This shipping script hides shipping options that start with the string EXPRESS.

Output.shipping_rates = Input.shipping_rates.delete_if do |shipping_rate|
  shipping_rate.name.upcase.start_with?("EXPRESS")
end

Rename a payment gateway

The following script renames the Cash on Delivery (COD) payment gateway to a custom name.

Input.payment_gateways.each do |payment_gateway|
  next unless payment_gateway.name == "Cash on Delivery (COD)"
  payment_gateway.change_name("Custom Name")
end

Output.payment_gateways = Input.payment_gateways

Discount codes

You can check a cart's discount code and apply any discounts as necessary.

cart_discounted_subtotal =
  case cart.discount_code
  when CartDiscount::Percentage
    if cart.subtotal_price >= cart.discount_code.minimum_order_amount
      cart.subtotal_price * ((Decimal.new(100) - cart.discount_code.percentage) / 100)
    else
      cart.subtotal_price
    end
  when CartDiscount::FixedAmount
    if cart.subtotal_price >= cart.discount_code.minimum_order_amount
      [cart.subtotal_price - cart.discount_code.amount, Money.zero].max
    else
      cart.subtotal_price
    end
  else
    cart.subtotal_price
  end

Stop customers from using a discount code

You can use a line item script to temporarily stop customers from applying a discount code at checkout. For example, during a Black Friday Cyber Monday sale, the following script will tell your customers that their discount code can't be used:

if Input.cart.discount_code
  Input.cart.discount_code.reject(
    message: "Maximum discount already applied! Discount codes can not be used during this promotion."
  )
end

Output.cart = Input.cart

If you have a Shopify Plus plan, you can remove the Discount code field from your checkout. To remove this field, add the following code to your checkout.liquid file.

<style>
[data-reduction-form="update"] {
  display: none
}
</style>
Ruby
# Use an array to keep track of the discount campaigns desired.
CAMPAIGNS = [
  # $5 off all items with the "sale" tag
  ItemCampaign.new(
    AndSelector.new(
      TagSelector.new("sale"),
      ExcludeGiftCardSelector.new,
    ),
    MoneyDiscount.new(5_00, "5$ off all items on sale",),
  ),

  # 10% off all items with a price lower than $100
  ItemCampaign.new(
    AndSelector.new(
      ExcludeGiftCardSelector.new,
      PriceSelector.new(:lower_than, Money.new(cents: 100_00)),
    ),
    PercentageDiscount.new(10, "10% off all items under 100$"),
  ),

  # Give every 3rd item with the tag "letter" for free
  BogoCampaign.new(
    TagSelector.new("letter"),
    PercentageDiscount.new(100, "Third item is free"),
    LowToHighPartitioner.new(2,1),
  )
]

# Iterate through each of the discount campaigns.
CAMPAIGNS.each do |campaign|
  # Apply the campaign onto the cart.
  campaign.run(Input.cart)
end

# In order to have the changes to the line items be reflected, the output of
# the script needs to be specified.
Output.cart = Input.cart

Other examples

Other script examples:

Learn more

Learn more about:

Want to discuss this page?

Visit the Shopify Community

Ready to start selling online with Shopify?

Try it free