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

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

问题描述

我的 WooCommerce 商店中有一些产品很重(超过 20 公斤),需要通过某种运输方式运输.我想为所有包含重量超过 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 中隐藏特定产品的特定运输方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:在 WooCommerce 3 中提供免费送货时隐藏特定的统一 下一篇:将 WooCommerce 可变产品价格范围替换为“最高&qu

相关文章

最新文章