admin管理员组

文章数量:1290935

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 3 years ago.

Improve this question

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;
}
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 3 years ago.

Improve this question

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;
}
Share Improve this question edited Jun 9, 2021 at 15:35 Jacob Peattie 44.1k10 gold badges50 silver badges64 bronze badges asked Jun 9, 2021 at 15:17 Joey PayneJoey Payne 1
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: codeMake order notes field at woocommerce checkout only viewable on front end by Admin level user role