Php 隐藏Woocommerce中特定用户角色的特定配送方式

Php 隐藏Woocommerce中特定用户角色的特定配送方式,php,wordpress,woocommerce,user-roles,shipping-method,Php,Wordpress,Woocommerce,User Roles,Shipping Method,在Woocommerce中,我使用Woocommerce批发专业套件(来自IgniteWoo)和固定费率的Box Shipping插件将B2B添加到我们的eshop中 我正在尝试禁用特定用户角色、来宾和客户的固定费率箱装运。我在网上搜索后发现此代码: add_filter( 'woocommerce_package_rates', 'hide_shipping_for_user_role', 10, 2 ); function hide_shipping_for_user_role( $rate

在Woocommerce中,我使用Woocommerce批发专业套件(来自IgniteWoo)和固定费率的Box Shipping插件将B2B添加到我们的eshop中

我正在尝试禁用特定用户角色、来宾和客户的固定费率箱装运。我在网上搜索后发现此代码:

add_filter( 'woocommerce_package_rates', 'hide_shipping_for_user_role', 10, 2 );
function hide_shipping_for_user_role( $rates, $package ) {
// Role ID to be excluded
$excluded_role = "wholesale_customer";

// Shipping rate to be excluded
$shipping_id = 'table_rate_shipping_free-shipping';

// Get current user's role
$user = wp_get_current_user();
if ( empty( $user ) ) return false;

if( in_array( $excluded_role, (array) $user->roles ) && isset( $rates[ $shipping_id ] ) )
unset( $rates[ $shipping_id ] );

return $rates;
}
对于客人和客户角色,我应该使用什么来代替“
批发\客户
”和“
表格\费率\配送\免费配送”
”,这样就不会显示固定费率箱配送


非常感谢您的帮助。

更新2:

您可能需要在“装运选项”选项卡下的“常规装运设置”中“启用调试模式”,以禁用临时装运缓存。

有关信息:用于“统一费率箱”的装运方法ID是
统一费率箱

以下代码将禁用“来宾”(未登录用户)和“客户”用户角色的“统一费率箱”装运方法:

add_filter( 'woocommerce_package_rates', 'hide_specific_shipping_method_based_on_user_role', 30, 2 );
function hide_specific_shipping_method_based_on_user_role( $rates, $package ) {

    ## --- Your settings --- ##
    $excluded_role = "customer"; // User role to be excluded
    $shipping_id = 'flat_rate_boxes'; // Shipping rate to be removed

    foreach( $rates as $rate_key => $rate ){
        if( $rate->method_id === $shipping_id ){
            if( current_user_can( $excluded_role ) || ! is_user_logged_in() ){
                unset($rates[$rate_key]);
                break;
            }
        }
    }
    return $rates;
}
代码进入活动子主题(或活动主题)的function.php文件。测试和工作

别忘了启用回邮缓存