Example scripts

Scripts are incredibly flexible and can be used to create unique and powerful discounts. Here are some examples of discounts scripts:

Percentage off all items

This 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_price(item.price * 0.9, message: "10% off all items!")
end

Output.cart = Input.cart

Default script

When you create a new script, the Source code box is filled with a default script. The default shows an example of how to run discount campaigns on multiple products.

# A class that knows how to calculate the discount for a line item, given a
# number of cents to discount each unit of item by.
class MoneyDiscount
  def initialize(cents)
    @amount = Money.new(cents: cents)
  end

  def calculate(line_item)
    @amount * line_item.quantity
  end
end

# A class that knows how to calculate the discount for a line item, given a
# number representing the percentage to discount each unit of item by.
class PercentageDiscount
  def initialize(percent)
    # Calculate the percentage, while ensuring that Decimal values are used in
    # order to maintain precision.
    @percent = Decimal.new(percent) / 100.0
  end

  def calculate(line_item)
    line_item.line_price * @percent
  end
end

# A class that knows how to apply a discount over a cart, for line items that
# match a certain product ID.
class ProductDiscountCampaign
  # Initialize the instance with the discounted product ID, the Discount
  # instance that will be used to calculate the discount, and the message that
  # will be used to describe the discount.
  def initialize(product_id, discount, message)
    @product_id = product_id
    @discount = discount
    @message = message
  end

  # Apply the discount to a given Cart. Also prints debugging lines to the
  # console with the variant IDs of the line items being discounted and the
  # discount amounts being applied.
  def run(cart)
    # Iterate through the line items in the cart.
    cart.line_items.each do |line_item|
      # Skip this line item unless it's associated with the target product.
      next unless line_item.variant.product.id == @product_id

      # Using the Discount instance, calculate the discount for this line item.
      line_discount = @discount.calculate(line_item)

      # Calculated the discounted line price using the line discount.
      new_line_price = line_item.line_price - line_discount

      # Apply the new line price to this line item with a given message
      # describing the discount, which may be displayed in cart pages and
      # confirmation emails to describe the applied discount.
      line_item.change_line_price(new_line_price, message: @message)

      # Print a debugging line to the console.
      puts "Discounted line item with variant \#{line_item.variant.id} by \#{line_discount}."
    end
  end
end

# Use an array to keep track of the discount campaigns desired.
CAMPAIGNS = [
  # Give the product "Glamourous T-Shirt" a 10% discount with the
  # discount message: "10% off Glamourous T-Shirt"
  ProductDiscountCampaign.new(
    12345,
    PercentageDiscount.new(10),
    "10% off Glamourous T-Shirt",
  ),

  # Give the product "Popular Pants" a $5 discount with the
  # discount message: "$5 off Popular Pants"
  ProductDiscountCampaign.new(
    67890,
    MoneyDiscount.new(500),
    "$5 off Popular Pants",
  ),
]

# 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

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.new(0)].max
    else
      cart.subtotal_price
    end
  else
    cart.subtotal_price
  end

Want to discuss this page?

Visit the Shopify Community

Ready to start selling online with Shopify?

Try it free