fbpx

Add 30% to your sales 💰 with upsells!Get App

Get 30% sales increase 💰 with upsells!Get App

How to add a free gift to Shopify cart when checkout total is over $25 (or any amount)

How to add a free gift to Shopify cart when checkout total is over $25 (or any amount)

Last Updated on February 28, 2021 by LAUNCHTIP

How to add a free gift to Shopify cart when checkout total is over $25

 

Think of how we’re going to accomplish these:

  • How would the free product scenario be tackled?
  • Add a product to the cart?

Free items can be treated in two ways:

  1. Including a new item with a price of zero. You may describe the quantity of a free product in this manner.
  2. Adding a discount to the free item that is required.
  3. Enable the Script Editor and write a basic script to render the price of the product $0 if placed in the cart.

We’re going to create a new snippet and call it “free-gift-inject.liquid.”

Introducing the variables:

  • Overprice free gift: Since Shopify deals in cents, you’ll need to multiply (amount*100) to get the cost.
  • Variant ID: To access the variant ID, you can update the variant to pick a product from the list of products.

We’ll submit a JSON request for the current cart objects, and then use AJAX to update the values on the /cart page:

{% assign free_gift_over_price_25 = 2500 %}
{% assign variant_id = '31059890110524' %}

Edit your cart.liquid after the snippet is generated to contain the created Shopify snippet. You can use cart-template.liquid instead of cart.liquid if it’s available.

{% comment %}



In /sections/cart-template.liquid, you will find the contents of the cart.liquid template.
{% endcomment %}
{% section 'cart-template' %}
{% include 'free-gift-inject' %}

 

If you’ve never used a script editor before, it’s fairly easy to get started. From the Shopify app store, you can add the Script Editor App. It has been added once;

  1. Navigate app
  2. Go to line Items
  3. Create script and select Blank script

Under code, add following:

FREEBIE_PRODUCT_ID = 31059890110524
	CART_TOTAL_FOR_DISCOUNT_APPLIED = Money.new(cents: 2500)
	DISCOUNT_MESSAGE = "Get a FREE gift for ordering $25 or more"
	

	freebie_in_cart = false
	cart_price_exceeds_discounted_freebie_amount = false
	cost_of_freebie = Money.zero
	

	# Test if the freebie is in the cart, also get its cost if it is so we can deduct from the cart total
	Input.cart.line_items.select do |line_item|
	  product = line_item.variant.product
	  if product.id == FREEBIE_PRODUCT_ID
	    freebie_in_cart = true
	    cost_of_freebie = line_item.line_price
	  end
	end
	

	# If the freebie exists in the cart, check the subtotal of the other items to see if the freebie should be discounted
	if freebie_in_cart
	  cart_subtotal_minus_freebie_cost = Input.cart.subtotal_price - cost_of_freebie
	  if cart_subtotal_minus_freebie_cost >= CART_TOTAL_FOR_DISCOUNT_APPLIED
	    cart_price_exceeds_discounted_freebie_amount = true
	  end
	end
	

	# Only true if the freebie is in the cart
	was_discount_applied = false
	if cart_price_exceeds_discounted_freebie_amount
	  Input.cart.line_items.each do |item|
	    if item.variant.product.id == FREEBIE_PRODUCT_ID && was_discount_applied == false
	      if item.quantity > 1
	        new_line_item = item.split(take: 1)
	        new_line_item.change_line_price(Money.zero, message: DISCOUNT_MESSAGE)
	        Input.cart.line_items << new_line_item
	        next
	      else
	        item.change_line_price(Money.zero, message: DISCOUNT_MESSAGE)
	      end
	    end
	  end
	end
	

	Output.cart = Input.cart

Related Posts
Leave a Reply