隐藏WooCommerce中特定运输类别的运输方法

编程入门 行业动态 更新时间:2024-10-10 10:27:04
本文介绍了隐藏WooCommerce中特定运输类别的运输方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在WooCommerce中,我使用 在WooCommerce中隐藏特定运输类别的运输方式 回答代码(第二种方式),但问题是我使用的WPML插件可以管理2种语言的网站,因此只查找一个类是不会做的。

In WooCommerce, I am hiding shipping methods based on different shipping classes in cart using "Hide shipping methods for specific shipping class in WooCommerce" answer code (the 2nd way), but the problem is that I use WPML plugin which manage 2 languages site, so looking for just one class won't do it.

所以我需要处理2个运输类而不是一个。我以这种方式尝试了addind 2航运类别:

So I need to handle 2 shipping classes instead of one. I tried addind 2 shipping classes this way:

// HERE define your shipping classes to find $class = 3031, 3032;

但是它破坏了网站。因此,我不仅要隐藏 3031 和 3032两种运输类别的已定义统一费率, code> 。

But it breaks the website. So I would like to hide the defined flat rate not only for both shipping classes 3031 and 3032.

我在做什么错?如何在不破坏网站的情况下启用2个运输类?

What I am doing wrong? How can I enable 2 shipping classes without breaking the web site?

推荐答案

要使用多个运输类,应首先在中定义它们一个数组,并在 IF 语句中,您将使用 in_array()条件函数:

To use multiple shipping classes, you should first defined them in an array and in the IF statement you will use in_array() conditional function this way:

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 ); function hide_shipping_method_based_on_shipping_class( $rates, $package ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // HERE define your shipping classes to find $classes = [3031, 3032]; // HERE define the shipping methods you want to hide $method_key_ids = array('flat_rate:189'); // Checking in cart items foreach( $package['contents'] as $item ) { // If we find one of the shipping classes if( in_array( $item['data']->get_shipping_class_id(), $classes ) ){ foreach( $method_key_ids as $method_key_id ){ unset($rates[$method_key_id]); // Remove the targeted methods } break; // Stop the loop } } return $rates; }

代码在您的活动子主题(或活动主题)的functions.php文件中)。经过测试并可以正常工作。

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

有时,您可能需要刷新进入运输区域的运输方法,然后禁用/保存并重新启用/保存您的统一费率送货方式。

Sometimes, you should may be need to refresh shipping methods going to shipping areas, then disable / save and re-enable / save your "flat rates" shipping methods.

相关主题:在WooCommerce中隐藏特定运输类别的运输方法

更多推荐

隐藏WooCommerce中特定运输类别的运输方法

本文发布于:2023-11-28 20:11:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643803.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类别   方法   WooCommerce

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!