• vissersj

    (@vissersj)


    I sell bicycles and parts. If someone buys a bicycle in the WooCommerce webshop only the shipping method ‘local_pickup’ should be visible. If someone buys a part only ‘flat_rate’ (shipping) should be available. If someone buys a bicylce and and a part only shipping method ‘local_pickup’ should be available. How should I solve this challenge with a new code snippet?

    I have two snippets that work perfectly, but only do one part of the job…

    // Hide 'Shipping' (flat_rate) with a bicylce in the shopping cart
        add_filter( 'woocommerce_package_rates', 'hide_shipping_based_on_tag' ,    10, 1 );
    
        function check_cart_for_share() {
    
    	// specify the category id's you want to hide flat_rate
    	$category_ID = array(
        '7', // Type A bike
        '8', // Type B bike
        '9', // Type c bike
        '10', // Type D bike
    
    	);
    	global $woocommerce;
    	$cart = $woocommerce->cart->cart_contents;
    
    	$found = false;
    
    	// loop through the array looking for the categories. Switch to true if the category is found.
    	foreach ($woocommerce->cart->cart_contents as $key => $values ) {
            $terms = get_the_terms( $values['product_id'], 'product_cat' );
            foreach ($terms as $term) {
                if( in_array( $term->term_id, $category_ID ) ) {
    
            $found = true;
            break;
        }
        }
        }
    
        return $found;
    
        }
    
        function hide_shipping_based_on_tag( $available_methods ) {
    
    	// use the function above to check the cart for the categories.
    	if ( check_cart_for_share() ) {
    
        // remove the method you want
        unset( $available_methods['flat_rate'] ); // Replace "flat_rate" with the shipping option that you want to remove.
          }
    
    	// return the available methods without the one you unset.
    	return $available_methods;
    
          }

    and

    // Hide local ickup (local_pickup) with parts in shopping cart
         add_filter( 'woocommerce_package_rates', 'hide_shipping_based_on_tag' ,    10, 1 );
    
         function check_cart_for_share() {
    
    	// specify the category id's you want to hide flat_rate
    	$category_ID = array(
        '11', // Parts
    
    	);
    	global $woocommerce;
    	$cart = $woocommerce->cart->cart_contents;
    
    	$found = false;
    
    	// loop through the array looking for the categories. Switch to true if the category is found.
    	foreach ($woocommerce->cart->cart_contents as $key => $values ) {
            $terms = get_the_terms( $values['product_id'], 'product_cat' );
            foreach ($terms as $term) {
                if( in_array( $term->term_id, $category_ID ) ) {
    
            $found = true;
            break;
        }
        }
        }
    
         return $found;
    
         }
    
         function hide_shipping_based_on_tag( $available_methods ) {
    
    	// use the function above to check the cart for the categories.
    	if ( check_cart_for_share() ) {
    
        // remove the method you want
        unset( $available_methods['local_pickup'] ); // Replace "local_pickup" with the shipping option that you want to remove.
          }
    
    	// return the available methods without the one you unset.
    	return $available_methods;
    
           }

    With kind regards,

    Sjors

    https://wordpress.org/plugins/woocommerce/

Viewing 1 replies (of 1 total)
  • Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    You only need one filter/function on woocommerce_package_rates.

    You should enable flat rate and local pickup in settings.

    Then your function hooked into woocommerce_package_rates should look for a bycycle, and if present in the cart it should just unset flat rate shipping. Thats all that should be needed.

    Since this is a request for customisation, if you need help ideally get a developer via jobs.wordpress.net so they can tailor it to you. I feat if you keep adding little snippets like above (which are not using 2.5 compatible functions) you’ll cause errors down the line.

Viewing 1 replies (of 1 total)
  • The topic ‘WooCommerce hide shipping method based on category advanced’ is closed to new replies.