Shopify Script to remove shipping option when products with a specific tag exists

If you want to remove a shipping option when specific products are in the cart then you can use Shopify Scripts.

For example, if dangerous goods are included that cannot be shipped using express shipping…

dangerous_goods_flag = false;
dangerous_goods_tag = 'Dangerous Goods';
shipping_rate_to_remove = 'EXPRESS';

Input.cart.line_items.each do |line_item|
  product = line_item.variant.product
  if line_item.variant.product.tags.include?(dangerous_goods_tag)
    dangerous_goods_flag = true;
  end
end

Output.shipping_rates = Input.shipping_rates.delete_if do |shipping_rate|
  if dangerous_goods_flag
    shipping_rate.name.upcase.start_with?(shipping_rate_to_remove)
  end
end