Sometimes you want to provide rates but limit their availability based on the weight of the order – for example if your carrier only carries packages up to a certain weight.

The WooCommerce Pro Shipping plugin already lets you set up tiered rates by weight, or item quantity, but there’s no way to set a maximum weight for a particular rate. Fortunately there are filters in place that let you achieve that fairly easily.

The example below limits a specific rate to a maximum weight of “5” – the units are whatever your store base weight unit is set to, e.g. if your store is set up to use kilograms, this will block the specific rate (ID 1 in our example below) over 5kg.

function limit_rate_by_weight( $availability, $rate, $rate_id ) {
    
    global $woocommerce;

    // The limit must be specified in your base store weight units.
    $limit = 5;

    // The rate ID of the rate you want to block.
    $blocked_rate_id = 1;

    // Retrieve the cart weight
    $weight = $woocommerce->cart->cart_contents_weight;

    if ( $rate_id == $blocked_rate_id ) {
        if ($weight > $limit) {
            return false;
        }
    }
    return $availability;
}
add_filter( 'woo_ps_availability', 'limit_rate_by_weight', 10, 3 );

This entry was posted in .
Bookmark the permalink.