Customize Distance Rate Shipping rule costs with code

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 top

Distance 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 top

This 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 );
↑ Back to top

Distance Rate Shipping also provides condition-specific rule cost filters for time, weight, order total, and quantity rules:

  • woocommerce_distance_rate_shipping_rule_cost_time_shipping
  • woocommerce_distance_rate_shipping_rule_cost_weight_shipping
  • woocommerce_distance_rate_shipping_rule_cost_order_total_shipping
  • woocommerce_distance_rate_shipping_rule_cost_quantity_shipping

Related Products

Enable customers to pay for products using a deposit or a payment plan.

Sell more bookings by presenting a calendar or schedule of available slots in a page or post.

Use of your personal data
We and our partners process your personal data (such as browsing data, IP Addresses, cookie information, and other unique identifiers) based on your consent and/or our legitimate interest to optimize our website, marketing activities, and your user experience.