Shipping Script: Not throwing error, but hinders normal cart checkout

Tourist
4 0 4

Hey Shopify community! I've written a simple script in Shipping script of script-editor. Based on some conditions we're offering free shipping. The issue is the script runs successfully, but when I update my cart using checkoutLineItemsReplace, then the cart is not getting updated. When I disable the script, then everything works as normal. Here's a script for reference:

 

# frozen_string_literal: true

CASHMERE_ITEMS = [2_067_473_662_063, 2_067_474_022_511, 4_340_428_636_271, 3_973_103_976_559, 4_344_866_209_903, 4_340_500_070_511,
                  4_340_364_640_367, 4_340_456_063_087, 4_350_209_097_839, 4_340_463_173_743, 4_367_579_709_551, 3_973_094_506_607,
                  4_340_400_128_111, 3_973_086_642_287, 3_974_863_290_479, 4_353_808_466_031].freeze

NO_OF_CASHMERE_APPAREL = 0

puts(Input.cart.discount_code)

Input.cart.line_items.each do |line_item|
  next unless CASHMERE_ITEMS.include? line_item.variant.product.id

  NO_OF_CASHMERE_APPAREL += line_item.quantity
end

ELIGIBLE_SERVICES = ['LB_SUPER'].freeze

# give them free shipping on the defined services.
if (NO_OF_CASHMERE_APPAREL >= 4)
  Input.shipping_rates.each do |shipping_rate|
    if ELIGIBLE_SERVICES.include?(shipping_rate.code)
      shipping_rate.apply_discount(shipping_rate.price, message: 'Free Express shipping!')
    end
  end
end

# Export the rates.
Output.shipping_rates = Input.shipping_rates

 

Please let us know if we're missing out on some trivial/key concept here.

0 Likes