Hide Shipping Options Woocommerce

So I’m trying to hide certain ship methods in Woocommerce based on a product tag. The main problem I face is my own lack PHP knowledge so I frankensteined the following code together with the help of some very friendly folks:

add_filter( 'woocommerce_available_shipping_methods', 'hide_shipping_based_on_tag' ,    10, 1 );

function check_cart_for_share() {

// load the contents of the cart into an array.
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;

$found = false;

// loop through the array looking for the tag you set. Switch to true if the tag is found.
foreach ($cart as $array_item) {
if (isset($array_item['product_tag']) && $array_item['product_tag'] == "CHOSEN_TAG") { // Replace "CHOSEN_TAG" with what ever tag you want
$found = true;
break;
}
}
return $found;

}

function hide_shipping_based_on_tag( $available_methods ) {

// use the function abve to check the cart for the tag.
if ( check_cart_for_share() ) {

// remove the rate you want
unset( $available_methods['flat_rate'] ); // Replace "flar_rate" with the shipping option that yu want to remove.
}

// return the available methods without the one you unset. 
return $available_methods;

}

I understand that this code is by no means universal and thus the variables will be different from case to case but perhaps someone can tell me if something looks off in the code. Much appreciated

Related posts

Leave a Reply

2 comments

  1. No doubt you have sorted this by now but your code was a good start for me … and since I sorted it I have published it below. Your problem was that woocommerce doesn’t have the product_tag in the cart array so you have to go and get it.

    /* !Hide Shipping Options Woocommerce */
    
    add_filter( 'woocommerce_available_shipping_methods', 'hide_shipping_based_on_tag' ,    10, 1 );
    
    function check_cart_for_share() {
    
    // load the contents of the cart into an array.
    global $woocommerce;
    $cart = $woocommerce->cart->cart_contents;
    
    $found = false;
    
    // loop through the array looking for the tag you set. Switch to true if the tag is found.
    foreach ($cart as $array_item) {
        $term_list = wp_get_post_terms( $array_item['product_id'], 'product_tag', array( "fields" => "names" ) );
    
        if (in_array("Heavy",$term_list)) { // Replace "Heavy" with what ever tag you want
    
            $found = true;
            break;
        }
    }
    
    return $found;
    
    }
    
    function hide_shipping_based_on_tag( $available_methods ) {
    
    // use the function above to check the cart for the tag.
    if ( check_cart_for_share() ) {
    
        // remove the rate 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;
    
    }
    
  2. You can use shipping class of Woocommerce to achieve this.

    Here is the step by step instructions.

    • Create a shipping class (woocommerce->settings->shipping->shipping classes).
    • Associate this shipping class with products (that you wand to hide shipping methods for)
    • Use this snippet. (copy and pate to function.php).

    For more information about the snippet is available here.