Discount script - Clear discounts, Browser sessions?

JCAndrea
Shopify Partner
41 2 13

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:

  1. The previously applied discounts stay in place, even after removing the trigger product (membership) from the cart.
    1. Is there a quick way to clear all previously applied line item discounts if the trigger product is removed?
  2. 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?
    1. 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
Replies 2 (2)

PaulNewton
Shopify Partner
6275 574 1324

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)

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Buy Paul a Coffee for more answers or donate to eff.org


JCAndrea
Shopify Partner
41 2 13

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