In the Woocommerce store there are 3 shipping options
- free shipping
- flat rate
- pickup
Inspired by Free shipping depending on weight and on minimal cart amount I would like to require a minimum total cart weight when choosing an shipping option.
- When customers order for 24kg or less, they only should be able to pickup their order.
- If the total cart weight is between 25kg and 49kg they should only may choose between the flat rate method or pickup.
- And if the total cart weight is 50 kg and above they may choose between free shipping and pickup.
On the internet I found WooCommerce: How to Setup Tiered Shipping Rates by Order Amount, which I customized to work properly on the store because we have 2 shipping zones (so multiple flat rates, local pickup, etc).
Unfortunately it does not work for my needs, while I think the code is correct? Any help will be much appreciated.
add_filter( 'woocommerce_package_rates', 'bbloomer_woocommerce_tiered_shipping', 9999, 2 );
function bbloomer_woocommerce_tiered_shipping( $rates, $package ) {
if ( WC()->cart->get_cart_contents_weight() < 25 ) {
if ( isset( $rates['local_pickup:5'], $rates['local_pickup:9'] ) ) unset( $rates['flat_rate:3'], $rates['flat_rate:6'], $rates['free_shipping:1'], $rates['free_shipping:8'] );
} elseif ( WC()->cart->get_cart_contents_weight() < 50 ) {
if ( isset( $rates['flat_rate:3'], $rates['flat_rate:6'], $rates['local_pickup:5'], $rates['local_pickup:9'] ) ) unset( $rates['free_shipping:1'], $rates['free_shipping:8'] );
} else {
if ( isset( $rates['free_shipping:1'], $rates['free_shipping:8'], $rates['local_pickup:5'], $rates['local_pickup:9'] ) ) unset( $rates['flat_rate:3'], $rates['flat_rate:6'] );
}
return $rates;
}