This snippet enables you to customize shipping costs dynamically based on the customer’s shipping address.
add_filter('woocommerce_package_rates', 'custom_shipping_rates_based_on_location', 10, 2);
function custom_shipping_rates_based_on_location($rates, $package) {
if ($package['destination']['country'] === 'US') {
foreach ($rates as $rate_key => $rate) {
if ('flat_rate' === $rate->method_id) {
$rates[$rate_key]->cost += 5; // Add $5 to flat rate shipping
}
}
}
return $rates;
}