SOF#AGSSOF#AGS
calculate shipping based on cart total

by softags

All Ecommerce website comes across this common issue where in they need to calculate shipping based on the cart total. To Solve this issues, We have to either use a plugin or do it manually to calculate shipping based on cart total.
What you need to do is give your 10 mins of time, login in to your wordpress dashboard and get excited for the magic !!

We are going to calculate shipping based on cart total without using a plugin.

Interesting !!

We are not going to use any plugin to solve this problem as sometimes lot more plugins in a wordpress website can slow down your  website, So by doing a little bit of coding is the best way to solve your most of the problems.

Today we will show how to calculate shipping based on cart total:

So for example purpose, We take 3 instances of calculating shipping based on cart total:

  1. If the cart total is less than $200, then only allow local delivery.
  2. If the cart total is between $201-$500, then allow flat rate shipping.
  3. If the cart total is above $501, then allow free shipping

You can set the price anything you want according to your terms. I am just giving you a reference on how to calculate shipping based on cart total.

There is an option where in we can set default value for shipping, but unfortunately it is only available for free shipping only. We cannot use it for flat rate or for local delivery.

calculate shipping based on cart total

So before getting down to code you need to first enable all the 3 shipping methods from woocommerce -> Shipping-> Shipping Options.

So Now Just go to your store add some products and proceed to checkout, You will see all the shipping options available.

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

To calculate shipping based on the cart total, Hover on the appearance, and click on the editor tab.

You will see a list of files on the right hand side. Click on the theme functions file. See the image below:

calculate shipping based on cart total

Now add the following code at the end of your theme function.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 by clicking on the update file button.

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

You can also read our other blogs:  Hide Directory browsing in wordpress and premium wordpress theme for free

 

 

softags
About softags
We Craft Experiences that helps Brand Stand out. We are an Offshore Web Design and Web Development Company based in Mumbai that stands out in Mobile Development, Web Design, Graphic Design and Web Development in Borivali and Charni Road.
calculate shipping based on cart total
Calculate shipping based on cart total [Woocommerce]