Personalized checkout and custom promotions with Shopify Scripts
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:
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
completely new browser session retains
@JCAndrea wrote: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?
How did you make a new session? Clear all cookies or use a private browsing mode?
Was the next checkout urls actually different from the initial checkout urls? (back|foward browser history)
Contact paull.newton+shopifyforum@gmail.com for the solutions you need
Save time & money ,Ask Questions The Smart Way
Problem Solved? ✔Accept and Like solutions to help future merchants
Answers powered by coffee Thank Paul with a ☕ Coffee for more answers or donate to eff.org
Thanks for the reminder.... Jake O from the Shopify Script Editor team got back to me yesterday from a separate ticket I opened. Jake informed me that I wasn't using my boolean logic in a way that Ruby wanted me to (I'm new to Ruby). So, I changed my code to reflect that change any everything worked perfectly. Also, there was NO browser session issue... my code only made it appear as such.
This code is really only in an inefficient quick-test state(more looping than needed), but here is the updated code regardless. Thanks!
new logic: spell out boolean "true/false" don't use 0/1.
#create array of membership product IDs
arry_membership_prodIDs = [4810694164558,4836276764750,6132834468020]
#set indicator for membership exists in the cart
cart_contains_membership = false
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 = true
end
if cart_contains_membership == true
#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
As 2024 wraps up, the dropshipping landscape is already shifting towards 2025's trends....
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024In today’s interview, we sat down with @BSS-Commerce to discuss practical strategies f...
By JasonH Nov 13, 2024