Back to Blog

2024-08-09

shopifyscripts

Black Friday Cyber Monday Shopify Scripts for Free Gifts

Black Friday Cyber Monday Shopify Scripts for Free Gifts

Hey guys,

This script will allow you to offer a free gift if the price is over a certain price. I set it up here to allow for 4 items to be chosen to be a free gift. This will come along with a bit of work to setup the frontend, but bundling to offer a free gift has been really popular on Shopify lately and I wanted to start the dialog here.

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

discount_products = [xxxxxxxxxx, xxxxxxxxxx, xxxxxxxxxx, xxxxxxxxxx]

total = Input.cart.subtotal_price_was

discount = if total > min_discount_order_amount
  1
else
  0
end

message = "You've earned a free gift"
did_discount = false

if Input.cart.discount_code == nil && !Input.cart.subtotal_price_changed?
  Input.cart.line_items.each do |line_item|
    product = line_item.variant.product
    next if product.gift_card?
    next unless discount_products.include?(product.id)

    if discount == 1 && !did_discount
      new_price = Input.cart.subtotal_price_was - line_item.variant.price
      unless new_price < min_discount_order_amount
        if line_item.quantity > 1
          new_line_item = line_item.split(take: 1)
          new_line_item.change_line_price(Money.new(cents: 0), message: message)
          did_discount = true
          Input.cart.line_items << new_line_item
        else
          line_item.change_line_price(line_item.line_price * (1-discount), message: message)
          did_discount = true
        end
      end
    end
    break
  end
end

Output.cart = Input.cart

This will ensure that if your cart is over $75 and one of the 4 product id's marked with x's is in the cart it will be free.