Hide Shipping Options Woocommerce

12,240

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;

}
Share:
12,240

Related videos on Youtube

user2539671
Author by

user2539671

Updated on September 27, 2022

Comments

  • user2539671
    user2539671 over 1 year

    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

  • Ian Purves
    Ian Purves over 10 years
    Actually what I was trying to do was remove a Shipping Class 'flat_rate' when another shipping class was present. This code enabled me to do it when a tag was present on the product. However when I woke up this morning I realised that if I changed 'product_tag' for 'product_shipping_class' (for more options see docs.woothemes.com/wc-apidocs/class-WC_Product.html) I could do what I wanted without having to add a tag as well as the shipping class ... it works ... but it will also work for any product attribute. Thought I would share. Ian
  • mirza
    mirza about 5 years
    woocommerce_available_shipping_methods is deprecated and not available use woocommerce_package_rates.