0

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;

}
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50

1 Answers1

1

Note: you cannot add multiple conditions to an if condition in the way you apply it.


Add the shipping options you want to keep under a certain condition to the $available_shipping array

  • When customers order for 24kg or less = local pickup
  • If the total cart weight is between 25kg and 49kg = local pickup and flat rate
  • If the total cart weight is 50 kg and above = local pickup and free shipping
function filter_woocommerce_package_rates( $rates, $package ) { 
    // Get cart contents weight
    $weight = WC()->cart->get_cart_contents_weight();
 
    // Conditions
    if ( $weight <= 24 ) {
        // Set available shipping options
        $available_shipping = array( 'local_pickup' );
    } elseif ( $weight > 24 || $weight < 50 ) {
        $available_shipping = array( 'local_pickup', 'flat_rate' );
    } else {
        $available_shipping = array( 'local_pickup', 'free_shipping' );
    }
    
    foreach ( $rates as $rate_key => $rate ) {
        // Targeting, NOT in array
        if ( ! in_array( $rate->method_id, $available_shipping ) ) {
            unset( $rates[$rate_key] );
        }
    }

    return $rates;
}
add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
  • Thank you for help! But unfortunately when I use this code it always shows only the pick up option. Am I missing something? – MediaCreandum Sep 01 '20 at 12:48
  • This code has been tested in WC 4.4.1 and works, you can always debug/print the `$weight` to see if it meets one of the other conditions. – 7uc1f3r Sep 01 '20 at 12:54
  • You could also try to refresh the shipping caches: In a shipping zone settings, disable / save any shipping method, then enable back / save. When this is done, you can (re)test it. – 7uc1f3r Sep 01 '20 at 12:59
  • I refreshed the cache of the shipping classes, but it didn't help. Unfortunately I don't know how to debug/print the `$weight` to see if it meets one of the other conditions. Any thoughts maybe? – MediaCreandum Sep 01 '20 at 13:32
  • 1
    No, that's all I can do from here. The answer works by default and if it does not work (completely) for you, you will have to [debug](https://www.php.net/manual/en/debugger.php) it to see where it goes wrong. – 7uc1f3r Sep 01 '20 at 13:34