Shipping Customization Functions let you modify shipping rates, hide/show shipping options, rename delivery methods, and add custom shipping logic based on cart contents, location, or customer data.
Generate a function that offers free shipping over $100:
1
Describe Your Function
Create a shipping customization function that renames the Standard Shipping option to show "Free Shipping" when cart total is over $100, and updates the rate to $0. Below $100, show normal shipping rates.
"Create tiered shipping discounts: Free shipping over $100, 50% off shipping for $75-$99, 25% off for $50-$74, normal rates below $50. Update titles to show 'Free' when applicable."
Product-Based Restrictions
Hide shipping options based on cart contents:
let has_oversized = input.cart.lines.iter() .any(|line| line.merchandise.product.has_tags(vec!["oversized"]));if has_oversized { // Only allow freight shipping for oversized items let operations = input.cart.delivery_groups .iter() .flat_map(|group| { group.delivery_options.iter().filter_map(|option| { if !option.title.to_lowercase().contains("freight") { Some(Operation::Hide(HideOperation { delivery_option_handle: option.handle.clone(), })) } else { None } }) }) .collect();}
Prompt Example:
"If cart contains products tagged 'oversized', hide all shipping methods except Freight Shipping. Show a message that oversized items require freight delivery."
Location-Based Pricing
Adjust shipping rates by destination:
let country = input.cart.delivery_groups[0] .delivery_address .country_code .as_str();let surcharge = match country { "AU" => 15.0, // Australia surcharge "NZ" => 12.0, // New Zealand surcharge "JP" => 20.0, // Japan surcharge _ => 0.0,};if surcharge > 0.0 { let operations = input.cart.delivery_groups .iter() .flat_map(|group| { group.delivery_options.iter().map(|option| { let current_price = parse_amount(&option.cost.amount); let new_price = current_price + surcharge; Operation::Update(UpdateOperation { delivery_option_handle: option.handle.clone(), price: Some(Decimal::from_f64(new_price).unwrap()), }) }) }) .collect();}
Prompt Example:
"Add $15 surcharge to all shipping methods for Australian customers, $12 for New Zealand, $20 for Japan. Keep other countries at normal rates."
Add Delivery Estimates
Show estimated delivery dates in shipping option names:
use chrono::{Utc, Duration};let operations = input.cart.delivery_groups .iter() .flat_map(|group| { group.delivery_options.iter().map(|option| { let days = if option.title.contains("Express") { 1 } else if option.title.contains("Standard") { 5 } else { 7 }; let delivery_date = Utc::now() + Duration::days(days); let formatted = delivery_date.format("%b %d"); Operation::Update(UpdateOperation { delivery_option_handle: option.handle.clone(), title: Some(format!( "{} (Arrives by {})", option.title, formatted )), price: None, }) }) }) .collect();
Prompt Example:
"Add estimated delivery dates to shipping method names. Express delivers in 1 day, Standard in 5 days, Economy in 7 days. Format as 'Method Name (Arrives by Nov 5)'"
Create a shipping function with tiers: free shipping over $100, 50% off shipping for $50-$99, normal rates below $50. Update the shipping method names to show the discount percentage.
Fragile Item Handling Fee
If cart contains any products tagged 'fragile', add a $5 handling fee to all shipping methods and update the name to show '(+ fragile handling fee)'.
Express for VIP Only
Hide Express Shipping for regular customers. Only show Express to customers tagged 'vip'. Keep Standard and Economy visible for everyone.
Remote Area Surcharge
For zip codes in remote areas (list: 99999, 88888, 77777), add $10 to all shipping methods and update name to show '(+ remote area surcharge)'.