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 :
add_filter( 'woocommerce_package_rates', 'businessbloomer_hide_regular_shipping_method', 10, 2 ); function businessbloomer_hide_regular_shipping_method( $rates, $package ) { $shipping_class_target = 35; // shipping class ID $in_cart = false; foreach( WC()->cart->get_cart_contents() as $key => $values ) { if( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) { $in_cart = true; break; } } if( $in_cart ) { // shipping method with value unset( $rates['wbs:2:d6f790a0_colissimo_sans_signature_classique'] ); unset( $rates['wbs:2:d748dcd4_lettre_suivie_classique'] ); unset( $rates['wbs:2:f1058bc8_colissimo_avec_signature_classique'] ); } else{ unset( $rates['wbs:2:1cdf4913_colissimo_sans_signature_print'] ); unset( $rates['wbs:2:fghla482_lettre_suivie_print'] ); unset( $rates['wbs:2:g27a1f56_colissimo_avec_signature_print'] ); } return $rates; }
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:
add_filter( 'woocommerce_package_rates', 'show_hide_shipping_methods', 10, 2 ); function show_hide_shipping_methods( $rates, $package ) { $shipping_class_id = 35; // Targeted shipping class ID $found = false; // Loop through cart items for the current package foreach ( $package['contents'] as $cart_item ) { if( $cart_item['data']->get_shipping_class_id() == $shipping_class_id ) { $found = true; break; } } // Loop through shipping rates foreach ( $rates as $rate_key => $rate ) { if ( $found && strpos($rate_key, '_classique') !== false ) { unset($rates[$rate_key]); } elseif (! $found && strpos($rate_key, '_print') !== false ) { unset($rates[$rate_key]); } } return $rates; }
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: