简体   繁体   English

在 Woocommerce 中隐藏特定产品的特定运输方式

[英]Hide specific shipping method for specific products in Woocommerce

I have a few products in my WooCommerce store that are to heavy (more than 20kg) to be shipped by a certain shipping method.我的 WooCommerce 商店中有一些产品很重(超过 20 公斤),需要通过某种运输方式运输。 I would like to hide 'shipping_method_0_flat_rate2' for all cart items which contain a product that is heavier than 20kg.我想为所有包含重于 20 公斤的产品的购物车项目隐藏'shipping_method_0_flat_rate2'

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.任何帮助表示赞赏。

You are making things more complicated in your code and your shipping method ID is not the good one… Try this instead:您在代码中使事情变得更加复杂,并且您的运输方式 ID 不是很好的…试试这个:

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;
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

You should need to refresh the shipping caches:您应该需要刷新运输缓存:
1) First this code is already saved on your function.php file. 1) 首先,此代码已保存在您的 function.php 文件中。
2) In Shipping settings, enter in a Shipping Zone and disable a Shipping Method and "save". 2) 在配送设置中,输入配送区域并禁用配送方式并“保存”。 Then re-enable that Shipping Method and "save".然后重新启用该运输方式并“保存”。 You are done.你完成了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 隐藏基于WooCommerce中产品或类别的特定送货选项 - Hide specific shipping options based on products or categories in WooCommerce 当 WooCommerce 提供免费送货时隐藏特定的送货方式 - Hide specific shipping method when Free Shipping is available in WooCommerce 在 WooCommerce 中隐藏特定运输方式的结帐运输字段部分 - Hide checkout shipping fields section for specific shipping method in WooCommerce 在 WooCommerce 中隐藏特定运输类别的运输方式 - Hide shipping methods for specific shipping class in WooCommerce 隐藏WooCommerce中特定运输类别的运输方法 - Hide shipping methods for specific shipping classes in WooCommerce 当 WooCommerce 中的另一种特定运输方式处于活动状态时隐藏特定运输方式 - Hide specific shipping method when another specific shipping method is active in WooCommerce 根据 WooCommerce 中的白天时间隐藏特定的运输方式 - Hide specific shipping method depending on day time in WooCommerce 在 Woocommerce 中根据国家/地区禁用特定产品的运输 - Disable shipping for specific products based on country in Woocommerce 在 Woocommerce 中禁用特定付款方式的运输方式 - Disable a shipping method for a specific payment method in Woocommerce 仅在 Woocommerce 中隐藏特定运输类别的免费送货 - Hide free shipping for specific shipping classes exclusively in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM