Posted on 17 Comments

Woocommerce Weight Based Shipping Without Plugin

Woocommerce Weight Based Shipping Without Plugin 1

Contents

4.5
(17)

Having a weight based shipping fee structure is very common in stores. However, this option is not support by default in WooCommerce. If you want to have weight-based shipping, you may need to use a plugin or you can go without one. However, without a plugin, you need to enter some code in the function.php file of your theme.

Are you ready? Let’s learn how to setup WooCommerce weight based shipping for free!

Some cautions before continuing

To enable WooCommerce weight based shipping, the first thing you need to do is to make sure your products to have weight. It is not a requirement, however, if your products don’t have weight, what’s the point of using weight-based shipping method? Some of your products don’t have weight is OK.

There is another caution you need to know that is which shipping zone is applied when calculating the shipping fee. If you have multiple shipping zones and one zone cover the other (for example, Vietnam is in Asia and you have two shipping zones, one for Vietnam and one for Asia), you need to know that the first shipping zone is applied.

Take a look at this screenshot:

Shipping zones order. Drag to top to have higher priority

The one at the top will be applied (Asia), not Vietnam 2. If you want to apply the shipping zone Vietnam 2, you need to hold the hamburger icon (underlined red) and drag it to the top position.

Our WooCommerce weight based shipping setup

In our example, we are going to have three weight based shipping tier:

  • For packages under 10kg, we charge $10
  • We will charge $20 for packages under 20kg (but more than 10kg)
  • And finally, for packages over 20kg, we charge $50

There is no limit on how many tiers you can set. You can have very complex tiers hierarchy. It all depends on you.

Setting up the shipping tiers in WooCommerce shipping settings

We are going to create a shipping zone that and three shipping tiers. You can proceed as follow:

Step 1: Create a new shipping zone by going to WooCommerce->Settings->Shipping zones and click on Add shipping zone. You then enter the name and regions that that you want to use the shipping tiers we are going to create.

Create shipping zone

Step 2: Create three flat rate shipping tiers

Now you can click on Add shipping method and select flat rate:

Add a flat-rate tier

Click on the Add shipping method button. You will see there is an option like this appears. Click on Edit.

Edit a flat rate tier

Step 3: Now, enter the details for the tier. As mentioned, we are going to create three tiers. In the image below, we create the last tier, shipping fee for packages that is more than 20kg. You can enter anything in method title. Note that this title will be displayed to your customer when they checkout:

Enter details for the flat rate tier

After repeating step 2 and 3 two more times, we have something like this:

All available tiers

Enter a code snippet in your theme’s functions.php

It is recommended to create a child theme and enter the code snippet in the functions.php file of that theme. However, if you just want to see the result, you can paste the code in your current theme’s functions.php file.

Now, let’s go to your theme functions.php file and enter this code:

add_filter( 'woocommerce_package_rates', 'binary_carpenter_weight_based_shipping_tiers', 10, 2 );
   
function binary_carpenter_weight_based_shipping_tiers( $rates, $package ) {
     

    if ( WC()->cart->cart_contents_weight <= 10 ) {
      
    foreach($rates as $key => $value)
    {
      if ($value->cost != 10)
        unset($rates[$key]);
        
    }
       
    } elseif ( WC()->cart->cart_contents_weight <= 20 ) {
       
    foreach($rates as $key => $value)
    {
      if ($value->cost != 20)
        unset($rates[$key]);
        
    }
       
    } else {
       
        foreach($rates as $key => $value)
    {
      if ($value->cost != 50)
        unset($rates[$key]);
        
    }
       
    }
   
  return $rates;
   
}

See the result in action

Now, you can add some products with weight to your cart and see the shipping tiers applied. In the example below, my total weight is 270kg and you can see that the highest tier is applied:

Woocommerce Weight Based Shipping Without Plugin 2

Use WooCommerce weight based shipping with local pickup

There are times you would want to add another method, local pickup for example. Then, you can replace the code above with this:

add_filter( 'woocommerce_package_rates', 'binary_carpenter_weight_based_shipping_tiers', 10, 2 );

function binary_carpenter_weight_based_shipping_tiers( $rates, $package ) {


    if ( WC()->cart->cart_contents_weight <= 10 ) {

        foreach($rates as $key => $value )
        {

            if ($value->cost != 10  && $value->method_id != 'local_pickup')
                unset($rates[$key]);

        }

    } else if ( WC()->cart->cart_contents_weight <= 20 ) {

        foreach($rates as $key => $value)
        {


            if ($value->cost != 20 && $value->method_id != 'local_pickup')
                unset($rates[$key]);

        }

    } else {


        foreach($rates as $key => $value)
        {

            if ($value->cost != 50  && $value->method_id != 'local_pickup')
                unset($rates[$key]);

        }

    }

    return $rates;

}

As you can see, in each if condition inside foreach, we check if the method has method_id as ‘local_pickup’. If it has, then we keep that method.

Now, on the cart page, you can see that the customer has another choice for shipping:

weight based shipping with local pickup

Conclusion

As you can see, setting up WooCommerce weight based shipping is quite simple with a little modification. If you find this post useful, please comment and share. Do you think this method can be improved? Please let me know.

Click on a star to rate it!

Average rating 4.5 / 5. Vote count: 17

No votes so far! Be the first to rate this post.

17 thoughts on “Woocommerce Weight Based Shipping Without Plugin

  1. Great. It works perfectly.
    How to add also a country check:

    WEIGHT COUNTRY COST
    <10KG US $10
    10kg 10kg 20 KG US $50
    >20 KG US $60

  2. Sorry the table was not clear. So my goal is to set 10$ if weight is less then X and shipping is to US. The same weight has a shipping cost of X+5 if shipping to EU and so on
    Thank you.

    1. Hi Giacomo,

      Do you need just that or it’s just one of some cases you need to handle? If you need to handle complex shipping rate calculation. I would recommend using a table rate shipping plugin. It allows you to create shipping method inside zones and set shipping cost based on weight, quantity, cart value …

      There are many plugins out there that provide table rate shipping, one from woocommerce.com is here, which I think it’s very flexible.

      Otherwise, if you only need this specific case, let me work with the code and update the post with the solution.

  3. this code not working for my website

    1. Could you please elaborate?

  4. Hi there!
    I am alittle confuse on the snippet part. Sorry i am not good with coding. I have a website and just started learning this. Can you please guide me on the snippet part?

    My tiers are name differently:
    0.5kg~1kg Shipment
    1kg~2kg Shipment
    2kg ~3kg Shipment
    Self-Pick Up

    How do i change that snippet coding and when i add it into the function.php part, i enter it at the most bottom part right?

    Hope to hear from you soon

    Thanks & Regards,
    Regina

    1. Hi Regina,

      You code you be like this:

      add_filter( 'woocommerce_package_rates', 'binary_carpenter_weight_based_shipping_tiers', 10, 2 );
      
      function binary_carpenter_weight_based_shipping_tiers( $rates, $package ) {
      
          $cart_weight = WC()->cart->cart_contents_weight;
      
          if (  $cart_weight < 0.5) {
      
              foreach($rates as $key => $value )
              {
      
                  if ($value->cost != 8.5  && $value->method_id != 'local_pickup')
                      unset($rates[$key]);
      
              }
      
          }
          else if ( $cart_weight >= 0.5 && $cart_weight <=1) {
      
              foreach($rates as $key => $value )
              {
      
                  if ($value->cost != 10  && $value->method_id != 'local_pickup')
                      unset($rates[$key]);
      
              }
      
          } else if ( $cart_weight >= 1 && $cart_weight <=2 ) {
      
              foreach($rates as $key => $value)
              {
                  if ($value->cost != 12 && $value->method_id != 'local_pickup')
                      unset($rates[$key]);
              }
      
          } else if ( $cart_weight >= 2 && $cart_weight <=3 ){
      
      
              foreach($rates as $key => $value)
              {
      
                  if ($value->cost != 13  && $value->method_id != 'local_pickup')
                      unset($rates[$key]);
      
              }
      
          } else
          {
              foreach($rates as $key => $value)
              {
      
                  if ($value->cost != 20  && $value->method_id != 'local_pickup')
                      unset($rates[$key]);
      
              }
          }
      
          return $rates;
      
      }
      
      
      1. Dear Dat,

        Thank you so much! You had been a wonderful person to work with. Definitely the best person to look for when you are a total loss at coding haha!

        Thank you for helping me to fix my error with my complicated website. It works fine and well now so THANK YOU!

        Thanks & with love:
        Regina =D

  5. Hi, Thanks for sharing your snippets. It works perfectly 🙂
    Do you think it’s easy to add “Local pickup” option as well? I would be happy if you could help me.

    1. I’ve updated the post with local pickup shipping support. Please have a look 🙂

      1. Wow! This is exactly what I wanted to do. ?
        Thank you so much!!

        1. My pleasure 🙂

  6. This worked wonderfully! I used the local pickup option code. Thank you for this great info!

    1. You’re welcome, Lisa 🙂

  7. Does your snippet works per zone/country?
    I have 3 prices depending on the weight (30kg and >1000kg), but the prices are different per country, and for some country even per zipcode.

    I can add all the methods and setup per zone as WooCommerce documentation, but is there anyway your snippet supports automatically checking/comparing the country of buyer and fetch the price I have set on the method based on that country/zipcode?

  8. Hi,

    Is it possible to make one code for those of us that have different prizing depending on zones/country?

  9. is it possible to add 3 different calculations?

    First Class
    Second Class
    Local Pickup

Leave a Reply

Your email address will not be published. Required fields are marked *