I have the following shopify script which adds a 25% discount to products if multiple items are purchased. The script seems to work but I'm recieving a runtime error (undefined method '*' for nil) on line 17 (line_item.line_price * (discount * 10 / 100),)
Any idea what the problem may be?
#Count number of items in cart total_items_seen = 0 Input.cart.line_items.each do |line_item| #Run through items in cart next if line_item.variant.product.gift_card? total_items_seen += line_item.quantity #Count items end if total_items_seen > 1 #if more than 1 products purchased discount = 7.5 #75% of full price message = "You got 25% discount for buying multiple items!" end Input.cart.line_items.each do |line_item| next if line_item.variant.product.gift_card? line_item.change_line_price( line_item.line_price * (discount * 10 / 100), message: message, ) end Output.cart = Input.cart
I've found with stackflow question with a similar error and the solution was to add a value to the object in question but I'm not sure how this might be used within the context of Shopify: https://stackoverflow.com/questions/28783750/undefined-method-for-nilnilclass
I'm guessing something like this?
line_item.change_line_price( price ||= line_item.line_price price * (discount * 10 / 100), message: message, )