隐藏Woocommerce中特定产品的特定运送方式 [英] Hide specific shipping method for specific products in Woocommerce

查看:68
本文介绍了隐藏Woocommerce中特定产品的特定运送方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WooCommerce商店中有一些产品很重(超过20kg),需要通过某种运输方式运输。对于所有重量超过20公斤的购物车商品,我想隐藏’shipping_method_0_flat_rate2’

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;

}

感谢您的帮助。

推荐答案

您正在使代码中的事情变得更加复杂,并且您的送货方式ID并不是一个好方法……尝试以下方法:

You are making things more complicated in your code and your shipping method ID is not the good one… Try this instead:

add_filter( 'woocommerce_package_rates', 'specific_products_shipping_methods', 10, 2 );
function specific_products_shipping_methods( $rates, $package ) {

    $product_ids = array( 113 ); // HERE set the product IDs in the array
    $method_id = 'flat_rate:2'; // HERE set the shipping method ID
    $found = false;

    // Loop through cart items Checking for defined product IDs
    foreach( $package['contents'] as $cart_item ) {
        if ( in_array( $cart_item['product_id'], $product_ids ) ){
            $found = true;
            break;
        }
    }
    if ( $found )
        unset( $rates[$method_id] );

    return $rates;
}

代码进入活动子主题(或活动主题)的function.php文件)。经过测试,可以正常工作。

Code goes in function.php file of your active child theme (or active theme). Tested and works.


您需要刷新运输缓存:

1 )首先,此代码已保存在您的function.php文件中。

2)在运输设置中,输入运输区域并禁用运输方式和保存。然后重新启用该发货方式并保存。 您已完成。

You should need to refresh the shipping caches:
1) First this code is already saved on your function.php file.
2) In Shipping settings, enter in a Shipping Zone and disable a Shipping Method and "save". Then re-enable that Shipping Method and "save". You are done.

这篇关于隐藏Woocommerce中特定产品的特定运送方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆