为 Woocommerce 3 中的特定用户角色隐藏 "Ship to a different address"

Hide "Ship to a different address" for a specific user role in Woocommerce 3

使用的插件:"User Role editor" 添加额外角色。

创建了一个角色:"SpecialCust"

如果用户有此卷,我已尝试取消设置 "ship to different address",但它并没有像看起来那样真正提取代码。

function Unset_Shipping_adress() {
    // Get the user object.
    $user = get_userdata( $user_id );

    // Get all the user roles as an array.
    $user_roles = $user_meta->roles;

    // Check if the role you're interested in, is present in the array.
    if ( in_array( 'SpecialCust', $user_roles, true ) ) {
        add_filter( 'woocommerce_cart_needs_shipping_address', '__return_false');
    }
}

您的代码中有一些遗漏的部分,可以简化。请尝试以下操作:

add_action('init', 'unset_shipping_address' );
function unset_shipping_address() {
    // HERE the targeted user roles
    $user_roles = array('SpecialCust', 'SpecialCust2', 'SpecialCust3');

    // Check user roles to hide “Ship to a different address”
    foreach( $user_roles as $user_role ) {
        if ( current_user_can( sanitize_title( $user_role ) ) ) {
            add_filter( 'woocommerce_cart_needs_shipping_address', '__return_false');
            break; // Found, we stop the loop
        }
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。它应该有效。

Reminder: to hide “Ship to a different address” in Woocommerce we just use:

add_filter( 'woocommerce_cart_needs_shipping_address', '__return_false');