本文介绍了Woocommerce 3中基于运输类别的筛选运输方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找代码,以在购物车中(在其他产品中)选择了特定运输类别的产品(例如,仅取件)时,在结帐时筛选出除本地取货以外的任何运输方式。 。

I have been searching for code to filter out any shipping methods other than local pick up, on checkout, when a product that has a specific shipping class selected (Only pickup, ex.) is in the cart (among other products).

我只发现过时的代码,无法在WC3 +上运行。

I only found code that was outdated and does not work on WC3+.

推荐答案

当启用了特定运输类别的产品时,这里是过滤除本地提货以外的所有运输方式的方法:

Here is the way to filter out any shipping methods other than local pick up, when a product that has a specific shipping class enabled:

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

    $shipping_class = 64; // HERE set the shipping class ID
    $found = false;

    // Loop through cart items Checking for the defined shipping method
    foreach( $package['contents'] as $cart_item ) {
        if ( $cart_item['data']->get_shipping_class_id() == $shipping_class ){
            $found = true;
            break;
        }
    }

    if ( ! $found ) return $rates; // If not found we exit

    // Loop through shipping methods
    foreach( $rates as $rate_key => $rate ) {
        // all other shipping methods other than "Local Pickup"
        if ( 'local_pickup' !== $rate->method_id && $found ){

            // Your code here
        }
    }

    return $rates;
}

代码进入活动子主题(或活动主题)的function.php文件)。经过测试并有效

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

这篇关于Woocommerce 3中基于运输类别的筛选运输方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 05:24