Skip to content

Instantly share code, notes, and snippets.

@gedex
Created August 30, 2017 06:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 );
@gedex
Copy link
Author

gedex commented Aug 30, 2017

Example scenario:

1.	Within 30 miles of shop – Charged at $1 per mile for delivery 
2.	Within 5miles of shop AND order total over $100 – FREE delivery 

DRS 1.0.8 adds a bunch of new filters, including woocommerce_distance_rate_shipping_rule_cost_distance_shipping, to override rule cost when selected Condition is Distance. With above scenario, no need to set rule number 2 in the DRS table.

screen shot 2017-08-30 at 1 35 26 pm

The snippet above can be placed in your theme's functions.php or as a drop-in plugin.

@hackerwebdesign
Copy link

I think this is exactly what I need, but I want to use the woocommerce_distance_rate_shipping_rule_cost_quantity_shipping filter. Can you tell me what I need to get the quantity? $package['contents_cost'] would need to change to something like $package['contents_quantity']; is that correct? Where can I find what attributes are in the $package array?

@pawelcichonski
Copy link

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

@tadakongithub
Copy link

Great! When I use this, and when the purchase is actually more than $100 and within 5 miles of distance, in the shipping section of checkout table on checkout page, it says only "Local pickup". Do you know why this is happening or how I can fix this?

@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