根据Woocommerce 3中的运输类过滤运输方式

问题描述 投票:2回答:1

我一直在搜索代码以过滤除本地提货以外的任何运输方式,在结账时,当选择具有特定运输类别的产品(仅限提货,例如)在购物车(以及其他产品)中时。

我只发现过时的代码并且在WC3 +上不起作用。

php wordpress woocommerce shipping custom-taxonomy
1个回答
1
投票

以下是在启用特定运输类别的产品时筛选除本地提货以外的任何运输方式的方法:

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文件中。经过测试和工作

然后在StackOverFlow中,searching for recent answer with woocommerce_package_rates将允许您完成代码。

© www.soinside.com 2019 - 2024. All rights reserved.