1

I'm setting up a website with shipping but i have items that are collection on for all shipping zone and item that can be sent in all shipping zone.

So I have set up shipping classes for all the zones.

I am using "Hide shipping method for specific shipping classes in woocommerce" answer code and it is what I need.

But instead of putting in each flat_rate id is there a way I can target all the Flat Rate shipping methods, so when I add an other flat rate shipping setting, it will work for it, without having me making changes into the code.

I hope you understand what I am after. Any help is appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Moto Bid
  • 13
  • 2

1 Answers1

0

To use it for all "flat rate" Shipping methods and some other defined shipping methods, you will use the following instead:

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // HERE define your shipping class to find
    $class = 92;

    // HERE define the shipping methods you want to hide (others than "flat rate")
    $method_key_ids = array('local_pickup:3');

    $found = false;

    // Checking in cart items
    foreach( $package['contents'] as $cart_item ){
        // If we find the shipping class
        if( $cart_item['data']->get_shipping_class_id() == $class ){
            $found = true;
            break; // Stop the loop
        }
    }

    if( ! $found ) 
        return $rates;

    // Loop through shipping methods
    foreach( $rates as $rate_key => $rate ) {
        // Targetting "Flat rate" and other defined shipping mehods
        if( 'flat_rate' === $rate->method_id || in_array($rate->id, $method_key_ids) ) {
            unset($rates[$rate_key]);
        }
    }   

    return $rates;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Refresh the shipping caches: (required)

  1. This code is already saved on your active theme's function.php file.
  2. The cart is empty
  3. In a shipping zone settings, disable / save any shipping method, then enable back / save.
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi, Thanks for the code, but it will not work for me sorry, when I put in local_pickup in the $method_key_ids part it removes that shipping method, but it does not remove any flat_rate shipping? I have cleared all caches, and have re-done all my shipping zone settings, but it will not remove any flat_rate shipping? – Moto Bid Apr 15 '19 at 18:11
  • I have tried different things on this code but just can not get it to remove the flat_rate methods, I just don't know as I said it removes the local_pickup if that is put in the methods_key_ids array I have also put a flat_rate method in to this and it removed it, but the code just isn't removing the flat_rate methods automatically, any ideas?? – Moto Bid Apr 15 '19 at 20:05
  • @MotoBid I have tested this code and it works just as expected on my test server: When a specific shipping class is found in cart items, all flat rates are removed ad also the specified shipping method Ids… – LoicTheAztec Apr 15 '19 at 20:33
  • Hi, The code should work but it just will not work for me, I am setting this up on a multi vendor site, where I setup the shipping zones & shipping classes, and then vendors on my site set there own flat_rate shipping methods, would this stop this code from working in any way?? as I say when I put information in the method_key_ids they get removed but all my flat_rate methods stay? I just don't understand it.. – Moto Bid Apr 16 '19 at 09:30
  • 1
    Thanks for your help in this matter, I could not get this code to work so I looked at another one you worked on and found that "Unsetting shipping rates if a shipping class applies to all cart items in WooCommerce" worked for me, I did modify it a little to remove the local pickup as well, it may not be the best coding but it works for me, I am glad you found the time to help with this and I thank you. – Moto Bid Apr 16 '19 at 19:35