0

I have a snippet of code that allows me to hide the order notes field at checkout for all users (logged in or not) but wanted to ask if someone has written a snippet that hides the same field from everyone, except Administrators.

Essentially, I want site administrator-level users to be able to add order notes when checking out, but no one else.

The current snippet is:

// Removes Order Notes Title - Additional Information & Notes Field
add_filter( 'woocommerce_enable_order_notes_field', '__return_false', 9999 );
// Remove Order Notes Field
add_filter( 'woocommerce_checkout_fields' , 'remove_order_notes' );
function remove_order_notes( $fields ) {
     unset($fields['order']['order_comments']);
     return $fields;
}

1 Answer 1

1

You could add an admin check with current_user_can(), something like this

function remove_order_notes( $fields ) {
    if (current_user_can('administrator')) return $fields;

    unset($fields['order']['order_comments']);
    return $fields;
}

If the current user is admin, return all fields.

Not the answer you're looking for? Browse other questions tagged or ask your own question.