简体   繁体   English

根据 Woocommerce 中的产品类别隐藏运输方式

[英]Hide shipping methods based on products categories in Woocommerce

With Woocommerce, I would like to hide all shipping methods except "Local pickup" when a defined products category is in cart…使用 Woocommerce,当已定义的产品类别在购物车中时,我想隐藏除“本地取货”以外的所有运输方式……

The code below does that for other product types, except variable products:以下代码对其他产品类型执行此操作,可变产品除外:

add_filter( 'woocommerce_package_rates', 'custom_shipping_methods', 100, 2 );
function custom_shipping_methods( $rates, $package ){

    // Define/replace here your correct category slug (!)
    $cat_slug = 'my_category_slug';
    $prod_cat = false;

    // Going through each item in cart to see if there is anyone of your category
    foreach ( WC()->cart->get_cart() as $values ) {
        $product = $values['data'];

        // compatibility with WC +3
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

        if ( has_term( $cat_slug, 'product_cat', $product_id ) )
            $prod_cat = true;
    }

    $rates_arr = array();

    if ( $prod_cat ) {
        foreach($rates as $rate_id => $rate) { // <== There was a mistake here
            if ('local_pickup' === $rate->method_id) {
                $rates_arr[ $rate_id ] = $rate;
            }
        }
    }
    return !empty( $rates_arr ) ? $rates_arr : $rates;
}

What can I do to make it work for variable products too?我该怎么做才能使它也适用于可变产品? Any help is appreciated.任何帮助表示赞赏。

I have revisited your code and here is correct way to make it work for variable products too:我已经重新访问了您的代码,这是使其也适用于可变产品的正确方法:

add_filter( 'woocommerce_package_rates', 'product_category_hide_shipping_methods', 90, 2 );
function product_category_hide_shipping_methods( $rates, $package ){

    // HERE set your product categories in the array (IDs, slugs or names)
    $categories = array( 'clothing');
    $found = false;

    // Loop through each cart item Checking for the defined product categories
    foreach( $package['contents'] as $cart_item ) {
        if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ){
            $found = true;
            break;
        }
    }

    $rates_arr = array();
    if ( $found ) {
        foreach($rates as $rate_id => $rate) { 
            if ('local_pickup' === $rate->method_id) {
                $rates_arr[ $rate_id ] = $rate;
            }
        }
    }
    return !empty( $rates_arr ) ? $rates_arr : $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运输方法基于运输类 - Conditionally Hide WooCommerce Shipping methods based on shipping class WooCommerce 运输方式 - 有条件隐藏(基于账单和运输国家) - WooCommerce Shipping methods- Conditional Hide(Based on Billing and Shipping countries) 根据 shipping class in WooCommerce 隐藏和取消隐藏特定的运输方式 - Hide and unhide a specific shipping methods based on shipping class in WooCommerce 显示/隐藏基于WooCommerce结帐字段值的送货方式 - Show/hide shipping methods based on a WooCommerce checkout field value WooCommerce - 根据购物车小计隐藏/显示运输方式 - WooCommerce - hide / show shipping methods based on cart subtotal 在 WooCommerce 中根据产品 ID 隐藏一组运输方式 - Hide a set of shipping methods based on product IDs in WooCommerce 根据 Woocommerce 中选择的运输方式显示隐藏付款方式 - Show hide payment methods based on selected shipping method in Woocommerce 隐藏WooCommerce中基于Variation产品属性的送货方式 - Hide shipping methods based on Variation product attribute in WooCommerce 隐藏基于WooCommerce中购物车总计的所有送货方式 - Hide all shipping methods based on cart items total in WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM