简体   繁体   English

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

[英]Filter Shipping method based on shipping class in 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).我一直在搜索代码来过滤除本地提货以外的任何运输方式,在结账时,当购物车中(以及其他产品)选择了特定运输类别(仅提货,例如)的产品时。

I only found code that was outdated and does not work on WC3+.我只发现过时的代码并且在 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;
}

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

Then in StackOverFlow searching for recent answer with woocommerce_package_rates will allow you to finish your code.然后在 StackOverFlow 中使用woocommerce_package_rates搜索最近的答案将允许您完成代码。

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

相关问题 Woocommerce,隐藏基于运输类的运输方式 - Woocommerce, hide shipping method based on shipping class Woocommerce 根据选择的运输类别在结账时更改运输方式标题 - Woocommerce Change shipping method title on checkout based on shipping class selected 在 WooCommerce 购物车中隐藏基于运费 class 的 COD 付款方式 - Hide COD payment method based on shipping class in WooCommerce cart 根据 Woocommerce 中购物车中的运费 class 价格项目更改运费 class - Change shipping class based on shipping class price items in cart in Woocommerce 根据 Woocommerce 中运送 class 计数的购物车项目更改运送 class - Change shipping class based on cart items shipping class count in Woocommerce Woocommerce:根据购物车商品的运输类别计数更改运输类别 - Woocommerce: Change shipping class based on cart items shipping class count 根据购物车商品的发货类别设置WooCommerce发货方式 - Unsetting WooCommerce shipping method based on cart items shipping classes 有条件隐藏WooCommerce运输方法基于运输类 - Conditionally Hide WooCommerce Shipping methods based on shipping class 基于运输等级和 Woocommerce 中的最低金额的有条件免费送货 - Conditional free shipping based on shipping class and minimal amount in Woocommerce 根据 WooCommerce 中的运输类别自定义计算的运输成本 - Custom calculated shipping costs based on shipping class in WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM