Unknown error on Shopify Scripts app

Shopify Partner
9 0 1

I'm trying to apply a fixed-amount discount to a cart that is over a certain amount, and I think I finally figured out the correct way to edit the cart subtotal. The issue is, I'm getting an error that I don't understand when I'm running the script. Here's the code:

class AmountsDiscountCampaign
  
  def initialize(cart1, discount1, cart2, discount2)
    @cart1 = Money.new(cents:100) * cart1
    @discount1 = Money.new(cents:100) * discount1
    @cart2 = Money.new(cents:100) * cart2
    @discount2 = Money.new(cents:100) * discount2
  end
  
  def run(cart)
    
    if cart.subtotal_price_was >= @cart1
      [cart.subtotal_price - @discount1, Money.new(0)].max
    elsif cart.subtotal_price_was >= @cart2
      [cart.subtotal_price - @discount2, Money.new(0)].max
    else
      cart.subtotal_price
    end
    
  end
  
end

CAMPAIGNS = [
  AmountsDiscountCampaign.new(100, 10, 50, 5)
]

CAMPAIGNS.each do |campaign|
  campaign.run(Input.cart)
end


Output.cart = Input.cart

And the error message:

[Error] undefined method 'fetch' for 0
  shopify/std_lib_mutable/core/money.rb:24:in Money.initialize
  Campaigns:13:in AmountsDiscountCampaign.run
  Campaigns:29:in Object.call
  Campaigns:28

Admittedly, fairly inexperienced with Ruby, but this seems like it should do what I want - I'm just not understanding what the error message is even referring to. Any help would be appreciated.

0 Likes
Shopify Expert
685 0 68

You can only discount line items, not the cart subtotal directly.

I'm a million different people
0 Likes
New Member
1 0 0

You should write like  "Money.new(cents: 0)"

0 Likes