Hello Shopify Expert and Support,
We’re looking for help in adjusting shipping rates based on specific product details. Specifically, we’re utilizing product-level metafields, focusing on the key ‘xyz’. Our objective is to detect, during the shipping price calculation, when a line item’s product metafield contains the key ‘xyz’. If this key is present, we aim to exclude the item from the standard shipping rate and instead apply a flat shipping price.
Could you please suggest any workaround ideas? Should we proceed with Shopify scripts or function APIs?
Kindly note that we’re on the Shopify Plus plan.
Here is my existing script. working with tags but not working on metafields
Input.shipping_rates.each do |shipping_rate|
next unless shipping_rate.source == “shopify”
def should_exclude_item(line_item)
Define your exclusion criteria here
For example, you can check for specific product tags, types, or metafields
line_item.variant.product.metafields.namespace.metakey.include?(‘exclude_shipping’)
end
Define your flat rate for excluded items
flat_rate_for_excluded_items = 5.00 # Customize as needed
Iterate through cart items and exclude items as necessary
excluded_items = Input.cart.line_items.select { |line_item| should_exclude_item(line_item) }
Calculate shipping rates for the remaining items in the cart
You can implement your own shipping rate calculation logic here
Output shipping rates
if excluded_items.empty?
If there are no excluded items, output regular shipping rates
Output.shipping_rates(rates: Input.shipping_rates)
else
If there are excluded items, add flat rate for each excluded item
excluded_items.each do |line_item|
Output.shipping_rates(
rates: [ShippingRate.new(
price: Money.new(cents: (flat_rate_for_excluded_items * 100)), # Convert flat rate to cents
title: “Flat Rate for Excluded Item: #{line_item.variant.product.title}”
)]
)
end
end
shipping_rate.apply_discount(shipping_rate.price * 0.10, message: “Discounted shipping”)
end
Output.shipping_rates = Input.shipping_rates