15 1 0 4000 1 https://codeblock.co.za 300 true 0
theme-sticky-logo-alt
How To Apply Volumetric Weight Shipping Fees in WooCommerce

How To Apply Volumetric Weight Shipping Fees in WooCommerce

0 Comments

Why Use Volumetric Weight in WooCommerce?

Understanding volumetric weight can have a significant impact on your bottom line. By accurately calculating shipping costs based on both weight and size, you can avoid surprises at the checkout and ensure you’re not overcharging or undercharging your customers for shipping.

How Does it Work in WooCommerce?

Now, let’s get down to it – how do you actually apply volumetric weight calculations in WooCommerce? Well, it’s easier than you might think. Here’s a step-by-step guide. You can add the following code into your theme functions file or as I prefer, via your own custom WordPress plugin.

Create the Base Function

<?php

function codeblock_woocommerce_package_rates($rates)
{
    global $woocommerce;

    $items = $woocommerce->cart->get_cart();
    // All our calculations and conditions will go here.
}
add_filter('woocommerce_package_rates', 'codeblock_woocommerce_package_rates', 1, 1);

Here we’re just creating our function that will attach to the woocommerce_package_rates filter-hook. This will filter the calculated shipping rates every time the cart is updated. Inside the function, we’re also getting the items from the cart and calling that $items.

Perform the Volumetric Weight Calculations

In the next step, we create an empty weights array. This will be used to store the weights of each item in the cart which we will later sum up.

After that, we need to loop through each item in the cart so we can get all the required information we need for our calculations like the quantity, dimensions, and actual weight.

Once we have all that information we can use the formula mentioned above to calculate the volumetric weight.

Depending on which is higher (the actual weight or the volumetric weight) will be pushed to the “weights” array.

When we’re done looping through the cart items, we some up the weights array and that will give us the total weight for this order. This total weight could be a combination of volumetric weights and actual weights depending on the product attributes.

Here’s what our code looks like so far:

<?php

function codeblock_woocommerce_package_rates($rates)
{
    global $woocommerce;

    $items = $woocommerce->cart->get_cart();

    // All our calculations and conditions will go here.
    $weights = [];

    foreach ($items as $item => $props) {
        // Check how many of this item is in the cart
        $qty = $props["quantity"];

        // Get the product object
        $product = wc_get_product($props['data']->get_id());

        // Get the actual weight
        $weight = !empty($p->get_weight()) ? $product->get_weight() : 0;

        // Calculate the volumetric weight
        $l = !empty($product->get_length()) ? $product->get_length() : 0;
        $w = !empty($product->get_width()) ? $product->get_width() : 0;
        $h = !empty($product->get_height()) ? $product->get_height() : 0;

        $volumetric_weight = (($l * $w * $h) / 5000) * $qty; // in Kg

        // Determine which is the higher weight that should be used
        $weights[] = $vol_weight > $weight ? $vol_weight : $weight;
    }

    // Sum up all the weights
    $total_weight = array_sum($weights);
}
add_filter('woocommerce_package_rates', 'codeblock_woocommerce_package_rates', 1, 1);

Calculate the New Shipping Fee Based on the Order Weight

Now, we need to apply the new cost for the shipping method. Some couriers will have a base cost up to a certain limit. For example, my courier in this example has a base cost of R100 up to 2kgs. So whether my parcel’s volumetric weight or actual weight exceeds that limit, an extra cost will be added based on the additional kgs. In this example, that extra cost is R6 per kg.

The extra weight is always rounded up. So if my parcel is 2.2kgs, I have to pay for 3kgs.

Once we’ve defined those specific values (limit for base price, cost per extra kg and the base shipping fee), we need to loop through the rates and the appropriate shipping method. In my case, I want to update the flate_rate shipping method, but you can apply this to any of you shipping methods.

In the condition ( if($total_weight < $max_kg_at_base_price) ), we check that whether or not the total weight of this order is less than the maximum kgs allowed for the base price. If it is, we update the rates cost to be that of the base price. If not, we need to do some additional calculations to determine what the rate should be. As follows:

  • Check how many kgs this order is above the weight in which the base fee would apply. This amount is always rounded up to the next kg.
  • Calculate how much extra the order will cost
  • Add the extra amount to the base fee
  • Update the shipping method’s fee.

Finally, we return our updated rates.

<?php

function codeblock_woocommerce_package_rates($rates)
{
    ...

    // Variable values depending on business rules and courier fees
    $max_kg_at_base_price = 2;
    $cost_per_extra_kg = 6;
    $base_shipping_fee = 100;
    
    foreach($rates as $key => $rate ) {

        // Change the rate for the flat_rate shipping method
        if($rates[$key]->method_id === "flat_rate") {
            
            if($total_weight < $max_kg_free) {
                $rates[$key]->cost = $base_shipping_fee;
            }
            else {
                // Check how many kgs this order is above the weight in which the base fee would apply (rounded up)
                $overrage = ceil($total_weight - $max_kg_at_base_price);
                // Calculate the fee above the base fee
                $fee_above = $overrage * $cost_per_extra_kg ;
                // The total fee
                $total_fee = $base_shipping_fee + $fee_above;

                // Update flat_rate
                $rates[$key]->cost = $total_fee;
            }
        }
    }

    return $rates;
}
add_filter('woocommerce_package_rates', 'codeblock_woocommerce_package_rates', 1, 1);

Full Function to Apply Volumetric Weight Calculations in WooCommerce

<?php

function codeblock_woocommerce_package_rates($rates)
{
    global $woocommerce;

    $items = $woocommerce->cart->get_cart();

    $weights = [];

    foreach ($items as $item => $props) {
        // Check how many of this item is in the cart
        $qty = $props["quantity"];

        // Get the product object
        $product = wc_get_product($props['data']->get_id());

        // Get the actual weight
        $weight = !empty($p->get_weight()) ? $product->get_weight() : 0;

        // Calculate the volumetric weight
        $l = !empty($product->get_length()) ? $product->get_length() : 0;
        $w = !empty($product->get_width()) ? $product->get_width() : 0;
        $h = !empty($product->get_height()) ? $product->get_height() : 0;

        $volumetric_weight = (($l * $w * $h) / 5000) * $qty; // in Kg

        // Determine which is the higher weight that should be used
        $weights[] = $vol_weight > $weight ? $vol_weight : $weight;
    }

    // Sum up all the weights
    $total_weight = array_sum($weights);


    // Variable value depending on business rules and courier fees
    $max_kg_at_base_price = 2;
    $cost_per_extra_kg = 6;
    $base_shipping_fee = 100;
    
    foreach($rates as $key => $rate ) {

        // Change the rate for the flat_rate shipping method
        if($rates[$key]->method_id === "flat_rate") {
            
            if($total_weight < $max_kg_at_base_price) {
                $rates[$key]->cost = $base_shipping_fee;
            }
            else {
                // Check how many kgs this order is above the weight in which the base fee would apply (rounded up)
                $overrage = ceil($total_weight - $max_kg_at_base_price);
                // Calculate the fee above the base fee
                $fee_above = $overrage * $cost_per_extra_kg ;
                // The total fee
                $total_fee = $base_shipping_fee + $fee_above;

                // Update flat_rate
                $rates[$key]->cost = $total_fee;
            }
        }
    }

    return $rates;
}
add_filter('woocommerce_package_rates', 'codeblock_woocommerce_package_rates', 1, 1);

Some Tips for Success

Now that you’ve got the basics down, here are a few additional tips to help you make the most of volumetric weight calculations in WooCommerce:

1. Regularly Review and Update Product Dimensions

As your product range evolves, don’t forget to review and update the dimensions accordingly. Adding new products or changing existing ones without updating their dimensions could throw off your shipping calculations.

2. Consider Packaging Efficiency

Efficient packaging can help minimise the size of your shipments, potentially reducing volumetric weight and saving you money on shipping costs. Invest in packaging materials that provide adequate protection without adding unnecessary bulk.

3. Offer Shipping Options

Give your customers the freedom to choose their preferred shipping method based on speed and cost. Providing options like standard delivery, express shipping, and click-and-collect can cater to a range of preferences and budgets.

4. Monitor and Adjust

Keep an eye on your shipping expenses and your courier’s fees over time and be prepared to adjust your strategy if necessary. As carrier rates change and your business grows, you may need to tweak your shipping settings to ensure they remain cost-effective.

Wrapping Up

And there you have it – a complete guide to applying volumetric weight calculations in WooCommerce. By harnessing the power of dimensional weight, you can streamline your shipping process, improve cost accuracy, and provide a better experience for your customers. So what are you waiting for? Dive in and start saving those pennies!

Remember, if you ever get stuck, WooCommerce’s extensive documentation and support community are there to lend a helping hand. Happy selling!

Was This Helpful?

JavaScript Performance Best Practices and Tools for Optimisation
Previous Post
JavaScript Performance: Best Practices and Tools for Optimisation

0 Comments

Leave a Reply