Walter
Walter

Reputation: 363

Hide checkout billing fields based on user role

Question: how to add code that will only hide the billing fields based on the users role, in this case customer.

Using the following code we can hide billing details in cart from logged in users:

add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
    if( is_user_logged_in() ){
        exec($fields['billing']);
        $fields['billing'] = array();
    }
    return $fields;
}

Upvotes: 2

Views: 1315

Answers (2)

Luis Garcia
Luis Garcia

Reputation: 65

For some reason, this still shows the billing_first_name_field in my case. And also the Header. The following code works fine:

add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
    if( current_user_can('customer') ){
        exec($fields['billing']);
        unset($fields['billing']); //this was changed
    }
    return $fields;
}

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 254492

Using WordPress current_user_can() conditional function that works with user roles, like:

add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
    if( current_user_can('customer') ){
        exec($fields['billing']);
        $fields['billing'] = array();
    }
    return $fields;
}

Code goes in function.php file of your active child theme (or active theme). It should works.

Upvotes: 1

Related Questions