WP SITES

3087 Coded Tutorials & 293 Plugins

Show Hide Shipping Methods Based on Cart Totals in WooCommerce

This code enables you to conditionally display shipping methods by ID based on cart totals. In this case we set 150 as the total.

If the cart total is less than 150, shipping method ID 5 displays otherwise shipping method ID 7 displays for orders above 150.

add_filter('woocommerce_package_rates', 'switch_flat_rate_based_on_total_v3', 10, 2);
function switch_flat_rate_based_on_total_v3($rates, $package) {

    $cart_total = WC()->cart->get_cart_contents_total();
    $flat_rate_5 = 'flat_rate:5'; // ID for flat rate 5
    $flat_rate_7 = 'flat_rate:7'; // ID for flat rate 7

    if ($cart_total > 150) {
        if (isset($rates[$flat_rate_5])) {
            unset($rates[$flat_rate_5]);
        }
    } else {
        if (isset($rates[$flat_rate_7])) {
            unset($rates[$flat_rate_7]);
        }
    }
    
    return $rates;
}

Swap out the method IDs to match your shipping methods.

Find Shipping Method ID’s.

Plugin

This plugin extends the code above so you can :

  • Apply thresholds per product
  • Set shipping zone method ids easier

Not exactly what you’re looking for?

Get Help

Leave a Reply

New Plugins