Skip to content

Instantly share code, notes, and snippets.

@gedex
Created August 30, 2017 06:31
Show Gist options
  • Save gedex/0e444c1026604416ba8cad8ebaf4be6e to your computer and use it in GitHub Desktop.
Save gedex/0e444c1026604416ba8cad8ebaf4be6e to your computer and use it in GitHub Desktop.
Override rule cost for distance shipping when destination within 5miles of shop AND order total over $100
<?php
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 );
@Opticcell
Copy link

Opticcell commented Jun 23, 2022

Awesome! With this example, I was able to figure out distance and weight conditions using the cart contents weight variable. I needed to do multiple zones with dual rates based upon weight and this seems to work well. Oh, it seems that only one shipping zone (All Zones) is needed, even for multiple zones (custom code does the multiple zones work). I then added Distance Rate as a shipping method within it (you can limit zip codes here as well). Within the Distance Rate shipping method, I added a single distance rule with default values (I guess it just needs to be there for this custom code to work). I also added my shop address here.

<?php

add_filter( 'woocommerce_distance_rate_shipping_rule_cost_distance_shipping', function( $rule_cost, $rule, $distance, $package ) {
	$cart_weight = WC()->cart->cart_contents_weight;
	if ( $cart_weight <= 12000 && $distance <= 3 ) {
		$rule_cost = 40; // Zone 1 - Rate 1
	} elseif ( $cart_weight > 12000 && $distance <= 3 ) {
		$rule_cost = 45; // Zone 1 - Rate 2
	} elseif ( $cart_weight <= 12000 && $distance > 3.1 && $distance <= 6 ) {
		$rule_cost = 50; // Zone 2 - Rate 1
	} elseif ( $cart_weight > 12000 && $distance > 3.1 && $distance <= 6 ) {
		$rule_cost = 55; // Zone 2 - Rate 2
	}

  return $rule_cost;
}, 10, 4 );

@Sayar-tk
Copy link

Sayar-tk commented Dec 1, 2022

Hello. I'm interested in purchasing the WooCommerce Distance Rate Shipping plugin. I have a condition to make, would it be possible using to have this scenario: Delivery from restaurant: 1-3 miles & 20 pounds or above in basket = free delivery 1-3 miles & less than 20 pounds = 2.50 for delivery 1-3 miles & less than 12 pounds = no delivery

3.1-4.0 & 20 pounds or above in the basket = 1 pound delivery 3.1-4.0 & less than 20 pounds = 3.50 for delivery 3.1-4.0 & less than 12 pounds = no delivery

Is this doable in this plugin? If there needs to be a custom code, would you provide an example here? Thank you

Is it possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment