Personalized checkout and custom promotions with Shopify Scripts
bad decimal operation (0 x 4)
BOX_PERCENT = BOX_DISCOUNT / BOX_PRODUCTS
BOX_DISCOUNT = 21000
BOX_PRODUCTS = 32100
both BOX_DISCOUNT and BOX_PRODUCTS are derived from Shopify script editor objects eg:
BOX_PRODUCTS = line_item.line_price.cents
Despite the error, it generates the correct calculation:
BOX_PERCENT = 0.690625
But I'm wondering how to fix the error. If I change the calculation to the following it works fine:
BOX_PERCENT = BOX_DISCOUNT / 32100
So there must be something wrong with the formatting of BOX_PRODUCTS
After inspecting BOX_PRODUCTS with BOX_PRODUCTS.inspect I can see the number is a decimal:
#<Decimal:0x7fb946112140>
I've tried to convert it into a fixnum with .to_f, .to_i, .delete('.').to_i and %1 but that returns the error:
[Error] undefined method 'to_f' for #Decimal:0x7f260aa7c140
The original line_item.line_price object before I convert it to a decimal with .cents is:
Inspect: #<Money: "320$">
Class: Money
Hm,
have you already tried the following?
BOX_PERCENT = Decimal.new(BOX_DISCOUNT) / BOX_PRODUCTS
Decimal.new generates the error: Can't convert Money into Decimal
Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025Unlock the potential of marketing on your business growth with Shopify Academy's late...
By Shopify Mar 12, 2025Learn how to increase conversion rates in every stage of the customer journey by enroll...
By Shopify Mar 5, 2025