How can I resolve discount rounding issues in Shopify Scripts?

How can I resolve discount rounding issues in Shopify Scripts?

piperis
Visitor
1 0 1

I have a Scripts running at checkout that reduces the price of products based on customer and product tag. There is a strange issue with rounding though at times... So, a product listed at £7.49 with 20% discount reduces it to £5.99 (£5.992 technically). HOWEVER, if the customer increases the quantity to 3 units, the priceshows as £6 per unit. I think this is because 7.49 x 3 x 0.8 = 17.976 which is rounded to 17.98 then divided by 3 = 5.9933333, so it rounds to £6. 

 

I am looking for a way to modify this code so that the price per unit is £5.99, irrelevant of units and rounding. It should just ignore any digits after the 2nd decimal (so 3 units would be £17.97)

 

The bit of code that I think is doing this is....

 

  def apply(line_item)
    new_line_price = if @discount_type == :percent
      line_item.line_price * @discount_amount
    else
      [line_item.line_price - (@discount_amount * line_item.quantity), Money.zero].max
    end
   line_item.change_line_price(new_line_price, message: @discount_message)
  end
end

 

The full code was taken from here

https://help.shopify.com/en/manual/checkout-settings/script-editor/examples/line-item-scripts#produc...

Replies 0 (0)