In my WooCommerce store (using version 4.2.2), I would like to hide / show some shipping methods based on cart subtotal as follow:
- For less than 25 euros: show only shipping methods A and B,
- Between 25 and 49 euros: show only shipping methods C and D,
- For 50 euros or more: show only free shipping
Note shipping methods A, B, C and D are all "flat rate".
I have googled this and managed to get this trying the following code (I was just testing with one rate and one threshold):
add_filter( 'woocommerce_package_rates', 'hide_shipping', 10, 2 );
function hide_shipping( $rates, $package ) {
// Retrieve cart subtotal
global $woocommerce;
$cart_subtotal = $woocommerce->cart->get_subtotal();
if( $cart_subtotal > 25 ){
unset( $rates['flat_rate:7'] );
}
return $rates;
}
But the code has no effect. Where am I going wrong?