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

Filter Shipping method based on shipping class in Woocommerce 3(根据 Woocommerce 3 中的运输类别过滤运输方法)
本文介绍了根据 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

然后在 StackOverFlow 使用 woocommerce_package_rates 搜索最近的答案 将允许您完成代码.

Then in StackOverFlow searching for recent answer with woocommerce_package_rates will allow you to finish your code.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Codeigniter htaccess to remove index.php and www(Codeigniter htaccess 删除 index.php 和 www)
htaccess mod_rewrite part of url to GET variable(htaccess mod_rewrite url 的一部分到 GET 变量)
Replacing a querystring parameter value using mod_rewrite(使用 mod_rewrite 替换查询字符串参数值)
.htaccess in subdirectory #39;overriding#39; parent htaccess(子目录“覆盖父 htaccess 中的 .htaccess)
How to rewrite SEO friendly url#39;s like stackoverflow(如何像stackoverflow一样重写SEO友好的url)
Is it okay to have a very long .htaccess file?(有一个很长的 .htaccess 文件可以吗?)