0

在 WooCommerce 中,我使用隐藏 WooCommerce 中特定运输类别的运输方式答案代码(第二种方式)隐藏基于购物车中不同运输类别的运输方式,但问题是我使用管理 2 种语言网站的 WPML 插件,所以只找一门课是行不通的。

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

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

但它破坏了网站。所以我想隐藏定义的统一费率不仅适用于运输类别30313032.

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

4

1 回答 1

2

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

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

有时,您可能需要刷新前往配送区域的配送方式,然后禁用/保存并重新启用/保存您的“统一费率”配送方式。

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

于 2019-06-01T13:42:44.800 回答