Sometimes you want to provide rates but limit their availability based on the size of the order – for example if your carrier only offers a certain set of rates for packages up to a certain size. 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 quantity 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 of 5 items – the rate won’t be offered if the cart contains more than five items.

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

    if ( $rate_id == 1 ) {
        if ($woocommerce->cart->cart_contents_count > 5) {
            return false;
        }
    }
    return $availability;
}
add_filter( 'woo_ps_availability', 'limit_rate_by_item_count', 10, 3);

This entry was posted in .
Bookmark the permalink.