1

I am trying to only show 1 shipping method when the cart subtotal is more than £50.

The website already has a lot of plugins, so I don't want to add anymore.

Any help is greatly welcomed.

Sophie

First - I have based the code on this from this thread:

`// if free shipping is available, disable a specific shipping rate
add_filter( 'woocommerce_package_rates', 'hide_specific_shipping_rate_if_shipping_free_is_available', 10, 2 );
function hide_specific_shipping_rate_if_shipping_free_is_available( $rates, $package ) {
    if ( isset( $rates['free_shipping:2'] ) ) {
        unset( $rates['flat_rate:20'] );
    }
    return $rates;
}`

I adapted it to:


// if order is over £50, disable a specific shipping rate

add_filter( 'woocommerce_package_rates', 'hide_specific_shipping_rate_if_shipping_free_is_available', 10, 2 );

function hide_specific_shipping_rate_if_shipping_free_is_available( $rates, $order_total ) {
        if ( $order_total > 50 ) {
                unset( $rates['flat_rate:1'] );
                unset( $rates['flat_rate:9'] );
                unset( $rates['free_shipping:2'] );
            }
    return $rates;
}

This worked but now when the cart is below 50 it doesn't come back (I clear WC transients each time, have tried clearning page and browser caches too)

So I tried this:

`// if order is over £50, disable a specific shipping rate
add_filter( 'woocommerce_package_rates', 'hide_specific_shipping_rate_if_shipping_free_is_available', 10, 2 );
function hide_specific_shipping_rate_if_shipping_free_is_available( $rates, $order_total ) {
        if ( $order_total > 50 ) {
                unset( $rates['flat_rate:1'] );
                unset( $rates['flat_rate:9'] );
                unset( $rates['free_shipping:2'] );
            }
             else {
                isset( $rates['flat_rate:1'] );
                isset( $rates['flat_rate:9'] );
                isset( $rates['free_shipping:2'] );
}
    return $rates;
}`

Another option I tried is:

``
// if order is over £50, disable a specific shipping rate
add_filter( 'woocommerce_package_rates', 'hide_specific_shipping_rate_if_shipping_free_is_available', 10, 2 );
function hide_specific_shipping_rate_if_shipping_free_is_available( $rates, $package ) {
    if ( $order_total > 50 ) {
        if ( isset( $rates['flat_rate:7'] ) ) {
        unset( $rates['flat_rate:1'] );
        unset( $rates['flat_rate:9'] );
        unset( $rates['free_shipping:2'] );
    }
    }
    return $rates;
}
``

I have read and tried other related threads too but they are either too different for me to adapt with my limited PHP knowledge, or they cause errors in functions.php


UPDATE

I've been doing some more testing, and it seems it works the first time, but when the cart is updated it doesn't change - so that is the part I guess that needs support on.

This code is a perfect example of that happening:

// if order is over £50, disable a specific shipping rate
add_filter( 'woocommerce_package_rates', 'hide_specific_shipping_rate_if_shipping_free_is_available', 10, 2 );
function hide_specific_shipping_rate_if_shipping_free_is_available( $rates, $order_total ) {
    if ( isset( $rates['flat_rate:2'] ) && $order_total > 50) {
        unset( $rates['flat_rate:1'] );
        unset( $rates['flat_rate:3'] );
    }
    return $rates;
}

Ignore the fact that the flat rates are different, my client has 2 sites and wants the same on both.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • I've been doing some more testing, and it seems it works the first time, but when the cart is updated it doesn't change - so that is the part I guess that needs support on. – Sophie Nicole Casey Aug 04 '23 at 04:26

1 Answers1

0

There is a mistake in your code, as there is no $order_total function variable argument, as it's $package instead (the current shipping package).

To get the cart subtotal, you can use:

  • Subtotal Including taxes: WC()->cart->subtotal,
  • Subtotal Excluding taxes: WC()->cart->get_subtotal().

So in your code:

add_filter( 'woocommerce_package_rates', 'hide_specific_shipping_rates_conditionally', 10, 2 );
function hide_specific_shipping_rates_conditionally( $rates, $package ) {
    if ( WC()->cart->subtotal > 50 ) {
        if ( isset($rates['flat_rate:1']) ) {
            unset($rates['flat_rate:1']);
        }
        if ( isset($rates['flat_rate:9']) ) {
            unset($rates['flat_rate:9']);
        }
        if ( isset($rates['free_shipping:2']) ) {
            unset($rates['free_shipping:2']);
        }
    }
    return $rates;
}

It should work.

To refresh, shipping rates, once the code is added/saved, empty your cart.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399