With WooCommerce Shipping Zones you can use simple PHP to accomplish lots of “advanced” shipping rules, such as shipping by weight.

With many of Business Bloomer fans always asking for this sort of customization, today we’ll see how to do just that. Without the need of just another plugin 🙂

1. Shipping by Weight – Requirements

If you or your client requires shipping by weight, then you first have to make sure ALL simple products and/or single variations have a weight > 0. Pretty simple, but sometimes people ask “Why shipping by weight doesn’t work?”, and the most common mistake is that products have 0 weight!

Also, under WooCommerce > Settings > Products make sure to select the correct “Weight Unit“, another typical error people make (defaults to Kg I believe).

2. Shipping by Weight – Shipping Methods Setup

You can add unlimited Flat Rates to each Shipping Zone. Also, you can rename each flat rate to something like “Orders Below 1kg”, “Orders Above 10kg”, etc to make the checkout label a little more user-friendly.

In our example, we will setup 3 weight tiers:

  • $10 shipping for orders up to 1kg
  • $20 shipping for orders up to 5kg
  • $30 shipping for orders above 5kg

And here is how to set the WooCommerce shipping methods:

  1. Flat Rate #1 > rename to “Orders Below 1kg” and assign cost = $10
  2. Flat Rate #2 > rename to “Orders Below 5kg” and assign cost = $20
  3. Flat Rate #3 > rename to “Orders Above 5kg” and assign cost = $30

Your final result for the specific zone (I called it “USA – Shipping by Weight”) will look like this:

WooCommerce Shipping by Weight (Shipping Zone Setup)WooCommerce Shipping by Weight (Shipping Zone Setup)
WooCommerce Shipping by Weight (Shipping Zone Setup)

3. Shipping by Weight – PHP Snippet

Now we need to “tell” WooCommerce that, based on the cart weight, a Flat Rate should be used instead of another. Only in this way we can show the correct flat rate to the end user.

First, take a note of the unique ID of the three flat rates. They should look look something like “flat_rate:9“. For more info on how to find them, check here: https://businessbloomer.com/woocommerce-disable-free-shipping-if-cart-has-shipping-class/#woocommerce-how-to-find-shipping-method-id-woo-2-6-after-the-introduction-of-the-new-shipping-zones

Second, let’s code! We’ll need to “unset” rates based on the total weight, so in total we’ll need 3 conditions: when weight is below 1 (kg), below 5 (kg) and above 5.

/**
 * @snippet       Shipping by Weight | WooCommerce
 * @sourcecode    https://businessbloomer.com/?p=21432
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_filter( 'woocommerce_package_rates', 'bbloomer_woocommerce_tiered_shipping', 9999, 2 );
   
function bbloomer_woocommerce_tiered_shipping( $rates, $package ) {
    
     if ( WC()->cart->get_cart_contents_weight() < 1 ) {
      
         if ( isset( $rates['flat_rate:5'] ) ) unset( $rates['flat_rate:6'], $rates['flat_rate:8'] );
      
     } elseif ( WC()->cart->get_cart_contents_weight() < 5 ) {
      
         if ( isset( $rates['flat_rate:5'] ) ) unset( $rates['flat_rate:5'], $rates['flat_rate:8'] );
      
     } else {
      
         if ( isset( $rates['flat_rate:5'] ) ) unset( $rates['flat_rate:5'], $rates['flat_rate:6'] );
      
     }
   
     return $rates;
   
}

And here is the proof:

WooCommerce: Shipping by Weight DemonstrationWooCommerce: Shipping by Weight Demonstration
WooCommerce: Shipping by Weight Demonstration
Share:

Leave a Reply