Posted on

WooCommerce Shipping by Weight – Code Snippet + Plugins

In this blog, we reveal the code to set up WooCommerce shipping by weight for flat rate & local pickup and also cover some plugins that help set advanced shipping rules based on weight.

WooCommerce shipping by weight blog featured image

Last updated on October 30, 2023

WooCommerce is quite flexible when it comes to setting up shipping zones and classes. It also allows you to set up a flat rate, free shipping and local pickup as shipping rate types.

Some stores charge a fixed rate for shipping, some vary their delivery costs based on the customer’s location. However, for products that vary considerably in size and weight, you can’t have a single flat fee for shipping.

You may want to opt for a solution or add some code to the shipping zones to set up WooCommerce shipping by weight and other advanced shipping rules.

Because there ain’t any provision with default WooCommerce to set up weight based shipping.

In this article, we will reveal the code you can use to set up weight based shipping and also some WooCommerce shipping plugins for advanced rules.

What is WooCommerce shipping by weight?

Weight based shipping enables you to set rates according to the weight of a product. Customers who purchase heavy items will pay more for shipping than those who buy smaller products.

Also, customers who purchase bulk items will also expect a nominal charge for shipping. Charging shipping per product quantity or for different products in an order is also not advisable.

With weight based shipping, you can charge a fair price for large orders and small ones alike. Moreover, you can avoid undercharging or overcharging your customers with shipping costs.

How to set up weight based shipping in WooCommerce without a plugin?

Before jumping to the code, make sure these requirements are met.

  • Your products should have a weight. All your simple products and/or single variations should have a weight > 0. Under WooCommerce > Settings > Products, make sure to select the correct ‘Weight unit’ (default is kg).
  • The first shipping zone is applied in case you have multiple shipping zones. Consider you have two shipping zones, one for Australia and one for Oceania. The shipping zone for Oceania will be applied as it is in the first position. To apply for a shipping zone for Australia, you need to hold the hamburger icon and drag it above Oceania.

Shipping by weight setup

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

  1. $10 for orders under 3 kg
  2. $15 for orders under 5 kg (but more than 3kg)
  3. $20 for orders above 5 kg

You can set unlimited tiers and unlimited flat rates for each shipping zone. It all depends on you.

Also, you can rename each flat rate to something like ‘Orders below 3 kg’, ‘Orders above 5 kg’, etc. to make the checkout label more user-friendly.

Set up shipping methods for all three like these:

  1. Flat Rate #1 > rename to ‘Orders below 3 kg’ and assign cost = $10
  2. Flat Rate #2 > rename to ‘Orders below 5 kg’ and assign cost = $15
  3. Flat Rate #3 > rename to ‘Orders above 5 kg’ and assign cost = $20
WooCommerce shipping by weight setup

If you need help, refer to this blog on setting up shipping rates and zones.

Shipping by weight code

Now based on the cart weight, a flat rate should be used instead of another. This is the only way you can show the correct flat rate to the end user.

Before proceeding, take a note of the unique ID of the three flat rates. They should look something like ‘flat_rate:9‘.

In our example, here are the following rate IDs:

  1. Flat Rate #1 > flat_rate:6
  2. Flat Rate #2 > flat_rate:7
  3. Flat Rate #3 > flat_rate:8

We’ll need to unset rates based on the total weight, so in total, we’ll need three conditions: when weight is below 3 kg, below 5 kg and above 5 kg. Here’s the code snippet:

add_filter( 'woocommerce_package_rates', 'storeapps_woocommerce_tiered_shipping', 9999, 2 );
function storeapps_woocommerce_tiered_shipping( $rates, $package ) {
    if ( WC()->cart->get_cart_contents_weight() < 3 ) {
       if ( isset( $rates['flat_rate:7'] ) && isset( $rates['flat_rate:8'] ) ) unset( $rates['flat_rate:7'], $rates['flat_rate:8'] );
    } elseif ( WC()->cart->get_cart_contents_weight() < 5 ) {
        if ( isset( $rates['flat_rate:6'] ) && isset( $rates['flat_rate:8'] ) ) unset( $rates['flat_rate:6'], $rates['flat_rate:8'] );
    } else {
        if ( isset( $rates['flat_rate:6'] ) && isset( $rates['flat_rate:7'] ) ) unset( $rates['flat_rate:6'], $rates['flat_rate:7'] );
    }
    return $rates;
}

Refer to this doc on how to safely add code snippets.

WooCommerce shipping by weight with local pickup

Instead of a flat rate shipping method, you may want to add local pickup for example. Then, you can replace the code above with this:

add_filter( 'woocommerce_package_rates', 'storeapps_woocommerce_tiered_shipping', 9999, 2 );
function storeapps_woocommerce_tiered_shipping( $rates, $package ) {
    if ( WC()->cart->get_cart_contents_weight() < 3 ) {
       if ( ( isset( $rates['flat_rate:7'] ) && 'local_pickup' !== $rates['flat_rate:7']->get_method_id() )
           && ( isset( $rates['flat_rate:8'] ) && 'local_pickup' !== $rates['flat_rate:8']->get_method_id() ) ) {
               unset( $rates['flat_rate:7'], $rates['flat_rate:8'] );
       }
    } elseif ( WC()->cart->get_cart_contents_weight() < 5 ) {
        if ( ( isset( $rates['flat_rate:6'] ) && 'local_pickup' !== $rates['flat_rate:6']->get_method_id() )
        && ( isset( $rates['flat_rate:8'] ) && 'local_pickup' !== $rates['flat_rate:8']->get_method_id() ) ) {
            unset( $rates['flat_rate:6'], $rates['flat_rate:8'] );
       }
    } else {
        if ( ( isset( $rates['flat_rate:6'] ) && 'local_pickup' !== $rates['flat_rate:6']->get_method_id() )
        && ( isset( $rates['flat_rate:7'] ) && 'local_pickup' !== $rates['flat_rate:7']->get_method_id() ) ) {
            unset( $rates['flat_rate:6'], $rates['flat_rate:7'] );
    }
    return $rates;
}

WooCommerce weight based shipping plugins

If you don’t want to get stuck with code, there are some powerful WooCommerce shipping by weight plugins.

These plugins allow you to create different shipping rates and conditions for your customers based on product weight. Let’s check some of them:

WooCommerce Weight Based Shipping

This is a free shipping plugin on the WordPress repository. It allows you to add multiple rules based on various conditions.

You can create as many shipping rules as you need for different order destinations, weight and subtotal ranges and set up a free shipping threshold.

You can also set shipping costs per specific shipping class individually.

The pro version of this plugin costs $19.

Get WooCommerce Weight Based Shipping

Table Rate Shipping

This plugin gives you highly customizable shipping options.

You can define multiple shipping rates based on weight, price, location, or item count.

You can also add rules based on weight and add costs per weight unit. The plugin’s intuitive interface makes it easy for users to manage their shipping strategies.

You can buy this plugin for $119 from the WooCommerce marketplace.

Get Table Rate Shipping

Conditional Shipping and Payments

Another shipping extension on WooCommerce that enables you to restrict the shipping options, payment options and shipping destinations available to your customers.

You can restrict shipping methods and live rates based on package weight, item count, or cart total.

You can also offer free shipping with subscription renewals using the plugin.

This plugin is quite popular and you can purchase it for $99.

Get Conditional Shipping and Payments

Flat Rate Box Shipping

This plugin allows you to define costs for boxes to different destinations. You can pack items into boxes based on the item size and volume, and then that box uses the fixed costs you define per destination.

You can add multiple box sizes per shipping zone, add costs per box and per weight unit and can also define a cost for items that cannot fit into the box.

This plugin is available to purchase from the WooCommerce marketplace for $59.

Get Flat Rate Box Shipping

Managing orders and products from one place based on shipping fields

Smart Manager plugin allows you to manage hundreds and thousands of orders from one place using an Excel-like sheet editor.

You can view and manage all your orders along with their shipping details like – shipping method, shipping state, shipping country, order shipping tax and other details.

Smart Manager orders dashboard

When you want to locate some orders based on a shipping method like ‘Flat rate’ amongst thousands of orders, use the Advanced Search option. Smart Manager will quickly fetch exact results for you.

You can also add new orders and custom orders directly to the sheet editor, set order status for it and also set the shipping method.

Another additional benefit is exporting orders based on shipping methods and other details.

Now, apart from orders, Smart Manager also allows you to filter products by weight, add weight to products in bulk, set and bulk edit prices for them as well.

Set weight in prices in bulk for products

You can also apply a filter that lists all your products weighing above X kgs, then update the regular price/sale price product field and all the changes will be applied instantly.

Moreover, with Smart Manager, you can:

  • Manage and bulk edit any post type – coupons, subscriptions, bookings, users, product add-ons, dropshipping, blog posts, pages, etc.
  • Delete records permanently or move them to trash – test products, test orders, test coupons…
  • Duplicate selected or all records for a specific post type
  • Export to CSV all records or based on filters for any post type
  • Set up Custom views(Column sets), provide access-restricted dashboards…

Try the live demo

Conclusion

Setting up WooCommerce weight based shipping without plugin is simple with some coding tweaks.

And a more simple way is to get a plugin from the list above to save time and hassles. All of them are popular and worth your money.

I hope you find this post useful. Any questions or suggestions, drop a line below.

FAQs

What if a customer orders multiple items with different weights, how does WooCommerce calculate the shipping cost?
WooCommerce calculates the shipping cost based on the total weight of all the items in the order and the shipping method selected by the customer.

What if a product’s weight changes after it has been added to a customer’s cart?
If a product’s weight changes after it has been added to a customer’s cart, the shipping cost may need to be recalculated and the customer may need to be notified of any additional charges or refunds.

Can the WooCommerce shipping weight plugin be integrated with third-party shipping carriers such as FedEx or UPS?
Yes.

What are some creative ways to use the weight-based shipping feature to offer discounts or promotions to customers?
As mentioned in the blog, you can set up tiered pricing based on weight, such as free shipping for orders over a certain weight threshold.
Another option is to offer discounted shipping rates for customers who purchase multiple items that may push their order weight into a higher tier.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.