• Trying to add a function to my child theme’s function.php file that will make the shopping cart page show just 1 shipping option if the cart only has items from a particular shipping class. Getting close but still down’t seem to quite have it working right. Any thoughts? Thanks.

    /*just show flat rate shipping method when only paper patterns are in the cart*/
    function only_show_flat_rate_shipping_with_patterns( $rates )	{
    
    	$targeted_class = 901; // <== the pattern ship shipping class
    	
        // Loop through cart items to find out if any hasn't the targetted shipping class
        foreach( WC()->cart->cart_contents as $key => $values ) {
            // IF One item is found without this shipping class
            if( $values[ 'data' ]->get_shipping_class_id() != $targeted_class ) {
                return $rates; // <== FOUND! We exit returning all Shipping rates
            } 
        }
    	
    	//if no exceptions to the shipping class, hide all methods but flat rate
    	unset( $rates['usps:D_PRIORITY_MAIL']);
    	unset( $rates['ups:4:01']);
    	unset( $rates['ups:4:02']);
    	unset( $rates['ups:4:03']);
    	
    	return $rates;
    	
    }
    
    add_filter( 'woocommerce_package_rates',     'only_show_flat_rate_shipping_with_patterns', 10, 2 );

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘function for filtering Woo Commerce shipping methods’ is closed to new replies.