Hide Shipping Options Woocommerce
Asked Answered
M

3

4

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

Mord answered 1/7, 2013 at 16:17 Comment(1)
Is the code not working? Or are you looking for more of how to make this better answer? If so then this question is better in codereview.stackexchange.com.Verbiage
M
6

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;

}
Maritime answered 2/11, 2013 at 16:12 Comment(2)
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. IanMaritime
woocommerce_available_shipping_methods is deprecated and not available use woocommerce_package_rates.Canonical
N
0

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.

Nicolis answered 4/10, 2016 at 11:49 Comment(0)
B
0

You can easily hide shipping methods without knowledge of PHP. A helpful plugin is available to hide shipping methods or rates depending on various conditions. You can check the Wordpress plugin here to hide shipping methods conditionally.

Burberry answered 17/10, 2024 at 4:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.