I have a few products in my WooCommerce store that are to heavy (more than 20kg) to be shipped by a certain shipping method. I would like to hide 'shipping_method_0_flat_rate2'
for all cart items which contain a product that is heavier than 20kg.
I tried to adjust the snippet below, but it is not complete and working:
add_filter( 'woocommerce_package_rates', 'hide_shipping_based_on_tag' , 10, 1 );
function check_cart_for_share() {
// specify the product id's you want to hide
$product_ID = array(
'113', // Product name
);
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;
$found = false;
// loop through the array looking for the products. Switch to true if the product 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, $product_ID ) ) {
$found = true;
break;
}
}
}
return $found;
}
function hide_shipping_based_on_tag( $available_methods ) {
// use the function above to check the cart for the products.
if ( check_cart_for_share() ) {
// remove the method you want
unset( $available_methods['shipping_method_0_flat_rate2'] ); // Replace with the shipping option that you want to remove.
}
// return the available methods without the one you unset.
return $available_methods;
}
Any help is appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…