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

Highlighted
Tourist
14 0 1

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.

0 Likes