We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Customizing Shipping Rates Based on Line Item Product Metafields

Customizing Shipping Rates Based on Line Item Product Metafields

khan_qasim007
Shopify Partner
1 0 0

 

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

 

Reply 1 (1)

MastersHub
Shopify Partner
216 21 38

There is more than one way to do this. We have development a solution similar to this before. Since the scripts will expire after a certain date, I recommend you solve them with Functions.

 

1. You can make shipping discounts according to certain criteria. (Shipping Discount API)

2. Hide delivery options for certain products or customers. (Delivery Customization API)

3. Generate local pickup options based on custom rules. (Pickup Option Generator API)

Helpful ? Like and Accept Solution
Buy me a Coffee
Need a Shopify Developer?
Send Email or Chat on WhatsApp