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
Thanks to everyone who participated in our AMA with 2H Media: Marketing Your Shopify St...
By Jacqui Sep 6, 2024The Hydrogen Visual Editor is now available to merchants in Shopify Editions | Summer '...
By JasonH Sep 2, 2024Note: Customizing your CSS requires some familiarity with CSS and HTML. Before you cust...
By JasonH Aug 12, 2024