Home Woocommerce Woocommerce – Hide a shipping method if cart total is higher than an amount

Woocommerce – Hide a shipping method if cart total is higher than an amount

by therichpost
0 comments
Woocommerce - Hide a shipping method if cart total is higher than an amount

Hello guys how are you? Welcome back to my channel. Today in this post I am going to show you Woocommerce – Hide a shipping method if cart total is higher than an amount.

Working live demo

Guys if you are new in WordPress or in WooCommerce then please check the below links for some good tutorials:

  1. WooCommerce Hooks
  2. WordPress Tricks
  3. WordPress Hooks

Guys here is the working code snippet for Woocommerce – Hide a shipping method if cart total is higher than an amount and please use carefully:

1. Guys here is the code snippet and you need to add your theme’s functions.php file:

add_filter( 'woocommerce_package_rates', 'shipping_based_on_country_subtotal', 100, 2 );
function shipping_based_on_country_subtotal( $rates, $package ) {
    $country   = WC()->customer->get_shipping_country();
    $subtotal  = WC()->cart->subtotal; // subtotal incl taxes
    $condition = $country == "US" && $subtotal >= 99; // <== HERE Set your condition (country and minimal subtotal amount)
    $free      = array(); // Initializing

    // Loop through shipping rates for current shipping package
    foreach ( $rates as $rate_key => $rate ) {
        if ( $condition ){
            $targeted_rate_id = 'flat_rate:2';
            
            if( $targeted_rate_id === $rate_key ) {
                unset($rates[$targeted_rate_id]);
            }
        }
    }
    return $rates;
}

This is it guys and if you will have any kind of query, suggestion or requirement then feel free to comment below.

Jassa

Developer’s King

Thanks

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.