Shopify Script Issue - Rounding

Branislove
New Member
1 0 0

Hello Shopify Community!

I am trying to fix an issue that keeps happening for discounts that we are running through Shopify Scripts.

Currently, the scripts gets the promo value and divides it with 100 to get a decimal value of the percentage with 2 decimal points max.

For an example, for a Promo value of %62.74, the calculation rounds it up to 0.62

 

PROMO = ""
cart = Input.cart.line_items

for variant in cart
     # Grab value of discount from String and formulate
     PROMO = variant.properties.inspect
     PROMO = variant.properties['PromoValue']
     PROMO = PROMO.to_i/100
     
     if PROMO != 0
        variant.change_line_price(variant.line_price * (1-PROMO) ,message: "#{PROMO}")
     else
 # if you did something else
     end
end

Output.cart = Input.cart

 

What I am trying to do is to actually calculate the discount based on the full percentage, which would be 0.6274 in this case.

Right now, it rounds it up to 0.62 for this scenario.

Any input would be great, thanks a lot!

Bran

0 Likes