• Resolved alexis888

    (@alexis888)


    Hello everyone !

    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 !

Viewing 1 replies (of 1 total)
  • Thread Starter alexis888

    (@alexis888)

    Solution found thanks to a Stack Overflow user. Code bellow and link to the thread on Stack here.

    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;
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Disable all shipping methods with a similiar term when item with shipping class’ is closed to new replies.