shopify-script-creator icon indicating copy to clipboard operation
shopify-script-creator copied to clipboard

Apply discount using line item's Compare At Price

Open vhvic opened this issue 1 year ago • 2 comments

We absolutely love this app, thank you so much for making scripting easy for small businesses like mine!

I have a feature request, or if someone can please advise:

How do we apply a discount using the line item's Compare At Price? We'd like to be able apply a 40% MSRP discount to all line items in a cart if a customer is tagged "VIPTAG"

I found this code which works in Shopify Scripts as-is, but can't figure out where to insert it so that it works with the rest of my desired qualifiers like the tagging function. I have a feeling this a probably a popular request, so please let me know if I'm just missing something in the existing dropdowns.

Input.cart.line_items.each do |line_item|
  compare_at_price = line_item.variant.compare_at_price
  new_line_price = compare_at_price - (compare_at_price * (Decimal.new(0.75) / 100.0))

  if new_line_price < line_item.line_price
    line_item.change_line_price(new_line_price, message: '75% off original price')
  end
end

Output.cart = Input.cart

vhvic avatar Nov 15 '23 20:11 vhvic

Hello, that is interesting! I haven't had that specific case of applying a discount on compare at price asked for before. Definitely possible to add to the script creator, but I am not sure when I could get to it.

Do you have an existing script you need to add it to or is this a new script?

If you wanted to use that and just add the condition for a tag, I think something like this should work:

if Input.cart.customer && Input.cart.customer.tags.include?("VIPTAG")
  Input.cart.line_items.each do |line_item|
    compare_at_price = line_item.variant.compare_at_price

    # Adding this to prevent errors when there is no compare at price
    next if compare_at_price.nil?
    
    # I modified this as the discount the other one applied doesn't seem right to me, but feel free to change it as needed
    new_line_price = compare_at_price - (compare_at_price * Decimal.new(0.40))

    if new_line_price < line_item.line_price
      line_item.change_line_price(new_line_price, message: '40% off MSRP')
    end
  end
end

Output.cart = Input.cart

jgodson avatar Nov 15 '23 23:11 jgodson

That worked perfectly thank you so much!

It'd be amazing if you do consider this option in the future, I can see so many use cases that we've been limited to by so far. Here's a few:

  1. (solved with your above code) Self-service discounts for industry "pros" where we can offer a blanket 40% discount off MSRP to tagged customers marked as "VIPTAG". It's been difficult since giving them a general Shopify discount code because it further discounts already "on sale" items available to the general public.

  2. Social media specific discount codes. We might try and incentivize a new social media customer with a special discount code where they can get 30% off MSRP on anything on the website for their first purchase, regardless if any of the current sale products appeal to them.

  3. Wholesale customers (solved with your above code). We could allow smaller wholesale customers to be self service without having to use an app.

(We use your app to basically run every sale on our site, and it just goes to show how amazing it is, because it was actually recommended to us by a Shopify Plus sales rep!)

vhvic avatar Nov 16 '23 20:11 vhvic