How to Calculate WooCommerce Flat Rate Shipping by Weight

How to Calculate WooCommerce Flat Rate Shipping by Weight
On this page
  1. Why This Workaround Beats Adding a Plugin
  2. Prerequisites
  3. The Weight-Based Calculation Snippet
  4. Troubleshooting
  5. Frequently Asked Questions
  6. Conclusion

To calculate WooCommerce flat rate shipping by weight without installing a dedicated shipping plugin, filter woocommerce_package_rates and overwrite a zero-cost Flat Rate method’s cost with a formula based on the cart’s total weight — effectively turning the built-in Flat Rate method into a per-kilogram calculator.

Why This Workaround Beats Adding a Plugin

  • It reuses WooCommerce’s existing Flat Rate shipping method rather than adding new shipping logic from scratch
  • No extra plugin overhead for a relatively simple weight-based pricing formula
  • It’s fully customizable to match whatever per-kilo or per-pound rate your carrier charges

Prerequisites

  • Every product must have a weight entered under Product Data > Shipping
  • Create a Flat Rate shipping method with a cost of 0 and note its rate ID (visible in the shipping zone settings, or discoverable via debugging)

The Weight-Based Calculation Snippet

PHP
/** Turn a zero-cost Flat Rate into a per-kilogram weight-based shipping charge */
add_filter( 'woocommerce_package_rates', 'calculate_flat_rate_by_cart_weight', 999, 2 );

function calculate_flat_rate_by_cart_weight( $rates, $package ) {

    $rate_per_kilo = 4.50;
    $flat_rate_id  = 'flat_rate:112'; // Replace with your actual Flat Rate ID

    if ( isset( $rates[ $flat_rate_id ] ) ) {
        $cart_weight = WC()->cart->cart_contents_weight;
        $rates[ $flat_rate_id ]->cost = $rate_per_kilo * ceil( $cart_weight );
    }

    return $rates;
}
Calculate WooCommerce flat rate shipping by weight: the page before adding the code
Before
Calculate WooCommerce flat rate shipping by weight: the page after adding the code
After

This example rounds up to the nearest whole kilogram before multiplying — adjust the rounding function (ceil, floor, or round) and the per-kilo rate to match your actual carrier pricing tiers.

Adding a Minimum Charge

PHP
function calculate_flat_rate_by_weight_with_minimum( $rates, $package ) {
    $rate_per_kilo = 4.50;
    $minimum_charge = 8.00;
    $flat_rate_id  = 'flat_rate:112';

    if ( isset( $rates[ $flat_rate_id ] ) ) {
        $cart_weight   = WC()->cart->cart_contents_weight;
        $calculated    = $rate_per_kilo * ceil( $cart_weight );
        $rates[ $flat_rate_id ]->cost = max( $calculated, $minimum_charge );
    }

    return $rates;
}
add_filter( 'woocommerce_package_rates', 'calculate_flat_rate_by_weight_with_minimum', 999, 2 );

Troubleshooting

The Rate Doesn’t Update

Empty the cart and re-add products before testing — shipping rates are cached in the session and won’t recalculate for an already-loaded cart.

Tax on the Shipping Rate Looks Wrong

If your Flat Rate method is set as taxable and your store has tax enabled, the tax calculation may need additional adjustment since this snippet only overwrites the base cost, not tax handling.

Can’t Find the Flat Rate ID

Temporarily add print_r( array_keys( $rates ) ); inside the filter function to output all available rate IDs for your shipping zone, then remove the debug line once you’ve identified the correct one.

Frequently Asked Questions

Is this a full replacement for a proper shipping-by-weight plugin?

For simple linear per-kilo pricing, yes — for tiered rates, multiple carriers, or dimensional weight, a dedicated plugin is more maintainable long-term.

Can I combine this with per-item shipping classes?

Yes — you’d extend the logic to check each cart item’s shipping class and apply different per-kilo rates accordingly, rather than one flat rate for the whole cart.

Does this work per shipping zone?

Yes — since the filter checks for a specific rate ID tied to a specific zone’s Flat Rate method, you can set up different per-kilo rates for different zones by repeating the pattern with each zone’s own rate ID.

Conclusion

Calculating WooCommerce flat rate shipping by weight without a plugin is achievable by overwriting a zero-cost Flat Rate’s price based on cart weight — a solid lightweight option as long as your pricing formula stays reasonably simple.

See also How to Set Up Shipping by Weight in WooCommerce Without a Plugin.

Prefer no code? All-in-One Utilities Woo Add-on handles this kind of change from a settings screen. Its Shipping by weight or order amount guide also shows how.

Leave a Reply

Schedule a call