This developer reference shows how to override a calculated Distance Rate Shipping rule cost with custom code.
Add custom code to a custom plugin or another site-specific customization layer. Test the change on a staging site before using it on a production store.
Scope of support:
We are unable to provide support for customizations under our Support Policy. If you need to customize a snippet or extend its functionality, we recommend working with a Woo Agency Partner or a WooCommerce developer on Codeable.
Filter hook
↑ Back to topDistance Rate Shipping applies condition-specific filters after a rule cost is calculated. For distance-based rules, use the woocommerce_distance_rate_shipping_rule_cost_distance_shipping filter.
The filter receives the calculated rule cost, the rule settings, the total distance or time value used for the rule, and the shipping package. When a rule uses a shipping class, the package passed to the filter is filtered to that shipping class.
Example: free shipping for nearby high-value orders
↑ Back to topThis example overrides the calculated cost for a distance rule when the destination is within 5 distance units of the shop and the package contents cost is more than 100.
add_filter( 'woocommerce_distance_rate_shipping_rule_cost_distance_shipping', function( $rule_cost, $rule, $distance, $package ) {
$order_total = $package['contents_cost'];
if ( $order_total > 100 && $distance <= 5 ) {
$rule_cost = 0;
}
return $rule_cost;
}, 10, 4 );
Related filters
↑ Back to topDistance Rate Shipping also provides condition-specific rule cost filters for time, weight, order total, and quantity rules:
woocommerce_distance_rate_shipping_rule_cost_time_shippingwoocommerce_distance_rate_shipping_rule_cost_weight_shippingwoocommerce_distance_rate_shipping_rule_cost_order_total_shippingwoocommerce_distance_rate_shipping_rule_cost_quantity_shipping