Script Tiered Discounts w/ collection condition

kmpoaquests
Excursionist
26 0 11
I'm trying to apply the following tiered discount, to all products belonging to the "Shop all promo" collection
 
Tiered discounts so if you spend x, you get y% off 
$250 = 10%
$500 = 15%
$750 = 20%
$1,000 = 25%

 

I appreciate any and all help with Shopify's Scripting app to create this discount.
Thank You again
 
Best Regards
Replies 5 (5)

Not applicable

Hi @kmpoaquests ,

 

First off thumbs up for using script editor, such a great app from Shopify. Here is a great example to do just what you're asking a tier discount based on total amount in the order:

 

https://help.shopify.com/en/manual/apps/apps-by-shopify/script-editor/examples#tiered-discounts-by-s...

 

Obviously you'll have to edit the code and make some adjustments but it's 90% there.

 

Once you install Script Editor, open the app and create a new line_item script and press the "Code" button. You can copy and paste the example script in there.

 

P.S. dollar values are literally in cents. So $50 would be 5000. I just had a test and boy it works great.

 

All the best,

 

Sam

kmpoaquests
Excursionist
26 0 11

I'm not code savvy to include a specific collection as a limiter , I can edit the percentage discount as that is simple enough.

 

Can anyone else help provide a solution please.

 

Thank You 

Not applicable

Hi @kmpoaquests ,

 

Yep I missed the filtering collections part, sorry about that. What I'm getting from documentation is that product collection is not currently available in Script API. So as an alternative a "product tag" could be used. Here is a working example just add "discount_eligible" as a tag to those products. 

 

# Define spending thresholds, from lowest spend to highest cart value.
SPENDING_THRESHOLDS = [
  {
    spend: 250 * 100,   # spend amount (in cents)
    discount: 10   # percentage discount
  },
  {
    spend: 500 * 100,
    discount: 15
  },
  {
    spend: 750 * 100,
    discount: 20
  },
  {
    spend: 1000 * 100,
    discount: 25
  }
]

# Find any applicable spending threshold.
eligible_threshold = nil
SPENDING_THRESHOLDS.each do |threshold|
  if Input.cart.subtotal_price_was.cents >= threshold[:spend]
    eligible_threshold = threshold
  end
end

# Apply threshold.
if !eligible_threshold.nil?
  Input.cart.line_items.each do |line_item|
    
    line_item.variant.product.tags.each do |tag|
      
      if tag == "discount_eligible"
        line_item.change_line_price(line_item.line_price * (1.0 - (eligible_threshold[:discount] / 100.0)), message: "#{eligible_threshold[:discount]}% off for purchases over $#{eligible_threshold[:spend] / 100}!")
      end
    end
  end
end

Output.cart = Input.cart

 

It would be nice to get the collection name and hopefully we see that soon in Scripts API, but some pros for the product tag would be not having to go back into the code to add a new collection when we can just update the product and it will be apart of the tiered discounts. 

 

All the best,

 

Sam

 

 

kpc_shop
Visitor
1 0 0

Can someone help me understand if I can use script editor for this same tiered discount (ie: Spend $100, get 15%, Spend $200, get 30%) but with a promo code attached?

Not applicable

Yes you can use script editor to effect the order like discount or line items. But look at Shopify’s automatic discounts I think you’ll like how easy it is to setup.