Shipping methods on the basis of cart total in WooCommerce | themelocation

Woocommerce: How to enable shipping methods on the basis of cart total?

In this tutorial; We will learn how to enable shipping methods on the basis of cart total.

We have three instances here:

1- If the Cart Total is less than $500, then only allow local delivery.

2- If Cart Total is between $501 – $1000, then allow Flat rate shipping.

3- If Cart Total is Above $1000, then allow free shipping.

There is a default option where we can set minimum threshold value to enable shipping method but unfortunately that option is for free shipping only.

2015-10-13_1217

So, first of all, I will enable all the three shipping methods from the Woo setting’s area.

2015-10-13_1224

Now, if you add products to cart by default you will see all shipping methods.

We need to enable shipping methods on the basis of price according to the scenario mentioned at top.

To do this add the following lines of code at the end of your theme’s functions.php file:

add_filter( 'woocommerce_package_rates', 'tl_shipping_on_price', 10, 2 );
function tl_shipping_on_price( $rates, $package ) {
 
    $total = WC()->cart->cart_contents_total;
	//echo $total;
    if( $total <= 500 ) {
 
        unset( $rates['flat_rate'] );
        unset( $rates['free_shipping'] );
 
    } elseif ( $total > 500 && $total < 1000 ) {
 
        unset( $rates['local_delivery'] );
        unset( $rates['free_shipping'] );
 
    } else {
		unset( $rates['local_delivery'] );
        unset( $rates['flat_rate'] );
	}
 
    return $rates;
}

Save the file after adding code by clicking on update file button.

Now go to cart again and you’ll see shipping methods on the basis of cart total.

Note: You can change amount values from the above code as per your requirements.

 

No comments yet.

Leave a Reply