Skip to content
Advertisement

Disable shipping methods having a specific word and based on a shipping class in WooCommerce

I’ve used a Business Bloomer snippet and tweaked it a bit to unset some shipping methods depending on a shipping class of items in the cart.

It works fine with the 4 shipping methods I have tested with but in order to fully work on my website, I have to list all shipping methods values manually and I have A LOT ( 86 to unset in if and 60 in else.)

Therefore, I would like to edit the snipped so I could unset all shipping methods that contains the same term all at once rather than look for each value individually, but don’t really know how to.

I have prints for sale and so I’ve set some shipping methods for orders including prints and some for orders whitout. Therefore all my shipping methods for prints have a value ending with “_print” when the others have a value ending with “_classique”, as you can see in the code bellow.

My goal is to disable all shipping methods ending with “_classique” when a product with the “print” shipping class in the cart and vice versa.

So far my code looks like this :

JavaScript

Any help on how to achieve this would be very appreciated. Thank you to anyone who’ll take time to read this !

Advertisement

Answer

Updated

Using PHP strpos() will allow you to check if a word is contained in a string. Then you will need to make some changes in your code to make it work:

JavaScript

Code goes in functions.php file of the active child theme (or active theme). It should works.

Clearing shipping caches:

  • You will need to empty your cart, to clear cached shipping data
  • Or In shipping settings, you can disable / save any shipping method, then enable back / save.

Related:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement