Personalized checkout and custom promotions with Shopify Scripts
Hi, I'm running Ruby script in checkout page to apply discount based on customer tag.
it seems to work fine, but shows Production errors : `Runtime Error`
It says like this.
RuntimeError undefined method '/' for #<Money: "96$">
Money value is being changed whenever this error happens.
I'd love to share some code piece here.
class PercentageDiscount def initialize(percent, message) @discount = (100 - percent) / 100.0 @message = message end def apply(line_item) # changed_amount = line_item.original_line_price - line_item.line_price # discounted_price = line_item.original_line_price * @discount # if changed_amount != Money.new(cents: 0) # discounted_price = discounted_price - changed_amount # end # line_item.change_line_price(discounted_price, message: @message) if line_item.properties.length == 0 line_item.change_line_price(line_item.line_price * @discount, message: @message) else is_subscribe = false line_item.properties.each do |property| if ( 1 == property.count { |x| x == "shipping_interval_frequency" } ) is_subscribe = true break end end if is_subscribe @discount = 100 - @discount * 100 if @discount > 10 discounted_price = ((line_item.line_price / 9.0) * 10 * (100 - @discount)) / 100.0 line_item.change_line_price(discounted_price, message: @message) end end end end end
It's really hard to find out it because I can't reproduce it in duplicated Script.
Hope to hear from Shopify experts soon.
All the best.
Thanks.
running into this same problem, and script editors half baked at best documentation makes working with script editor such a pain. I honestly hate the replacement they come up with too. Functions over complicates what should be a simple task. if they would have added even the tiniest bit of helpful documentation, maybe people would have utilized it more.
We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024Thanks 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, 2024