Personalized checkout and custom promotions with Shopify Scripts
We've included code that will discount a specified product when the user exceeds $50.00 in their cart. This works when the user only has one of the promo items in their cart, the promo item is fully discounted. However, when the user increases the quantity of the promo item, the promo item is no longer discounted at all -- in our script below, we have a split function that recognizes a quantity > 1 and should split off the promo product into its own line item and discount a single promo item.
In the "Run Script" box of the Script Editor app -- this works as intended when you set the promo item quantity to 2 and the cart total over $50.00. However on the front-end (when you click "preview on the Online Store") in the cart, the same functionality does not happen and the promo item is not discounted when at a quantity greater than 1.
Below is the script we are using:
FREEBIE_PRODUCT_ID = 901775294506 CART_TOTAL_FOR_DISCOUNT_APPLIED = Money.new(cents: 100) * 50 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: '') Input.cart.line_items << new_line_item next else item.change_line_price(Money.zero, message: '') end end end end Output.cart = Input.cart
Thank you
I believe the problem is with this block:
if product.id == FREEBIE_PRODUCT_ID
freebie_in_cart = true
cost_of_freebie = line_item.line_price
end
It should be using the price of the Variant, not the line_price. The line_price is the total price of the quantity * variant.price. So it should be as per below:
if product.id == FREEBIE_PRODUCT_ID
freebie_in_cart = true
cost_of_freebie = line_item.variant.price
end
Portrait of Stephen positioned next to an image of planet Earth, with the Stephen's World ...
By JasonH Mar 18, 2024Digital marketers and app developers have tracked activity in apps and websites for yea...
By Ollie Mar 13, 2024February was an exciting month with Shopify Editions, informative webinars, and more! F...
By JasonH Mar 7, 2024