Greetings-
I just discovered Script Editor, so maybe i’m missing something… I wrote a short script using the Script Editor app. The script applies a 10% discount on ALL items in the cart if a certain product (membership product) exists in the cart. But, the trigger product (membership) DOES NOT get the discount. I actually have two membership products that trigger the discount.
I got all of that to work great, but two things were odd to me:
-
The previously applied discounts stay in place, even after removing the trigger product (membership) from the cart.
- Is there a quick way to clear all previously applied line item discounts if the trigger product is removed?
-
And more concerning, a completely new browser session retains the discount, even if the ‘trigger’ product has NEVER been added to the cart on that computer or session?
- Does Script Editor do any browser session management in the backend, or do we have to write that into the script as well?
Thanks for any help!
Jason
#create array of membership product IDs
arry_membership_prodIDs = [4810694164558,4836276764750]
#set indicator for membership exists in the cart
cart_contains_membership = 0
Input.cart.line_items.each do |line_item|
#loop over cart items to see if a membership product ID exists
product = line_item.variant.product
next if product.gift_card?
next unless arry_membership_prodIDs.include?(product.id)
#this product exists in the array of membership IDs, set the cart indicator
cart_contains_membership = 1
end
if cart_contains_membership
#loop over all products again and only discount those products that DO NOT exist in
#the array of membership product IDs
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
next if product.gift_card?
if arry_membership_prodIDs.include?(product.id) == false
line_item.change_line_price(line_item.line_price * 0.90, message: "Member Discount")
end
end
end
Output.cart = Input.cart