Script error undefined method '/' for #<Money: "xx$">

Script error undefined method '/' for #<Money: "xx$">

Ryan1118
Excursionist
21 0 7

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.

Reply 1 (1)

JeffSpurlock
Shopify Partner
11 0 14

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.