Can we apply 75% discount on 'compare at price' using Script Editor?

lindabr
Visitor
1 0 0

Is there a way in Script Editor to discount a line item's compare at price instead of it's line price? We want to offer customers 75% off the original compare at price, but cannot figure out how to do it. We have only been able to offer an additional X amount off the current selling price and want to try something different. 

Reply 1 (1)

RobDukarski
Shopify Partner
88 15 20

@lindabr 

The first thing to note is that if the discounted price is not less than the current line price the discount should not apply.

You should be able to grab the compare at price of the variant and apply the 75% discount to that. From there you would verify if that resulting price is less than the current line price of the line item and that if it is apply the discount.

It would be something like this:

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


You may want to add additional conditions to target specific products or variants but without testing I believe something like that should work for you.

Hope that helps!

- Rob Dukarski
Helping to make commerce better for everyone!