0

Can any help with this?

I like the comma separated version because it's smaller to write and easy on the eyes, lol, but I can't make it work.

//** WORKING - Works on the Checkout page
add_action( 'woocommerce_cart_contents', 'pst_pdf_ceckout_page_button' );
function pst_pdf_ceckout_page_button() {
    if( current_user_can('administrator') || current_user_can('ABC') || current_user_can('LMN') || current_user_can('XYZ') ) {
echo '<a class="button bc-pst-pdf-btn" https://LINK TO FILE.pdf" target="_blank">PST Exempt PDF Form </a>';
} 
}


//** NOT WORKING - Won't show up on the Checkout page.
add_action( 'woocommerce_cart_contents', 'pst_pdf_ceckout_page_button' );

function pst_pdf_ceckout_page_button() {
    if ( current_user_has_role( array('administrator', 'ABC', 'LMN', 'XYZ') ) ) {
echo '<a class="button bc-pst-pdf-btn" href="https://LINK TO FILE.pdf" target="_blank">PST Exempt PDF Form </a>';
} 
}
1

The current_user_has_role() is not a WordPress core function, on the other hand there's wc_current_user_has_role( $role ) in WooCommerce where $role is a string, but 3rd party plugins are general off-topic.

Creating this function was closed as wontfix in this ticket: https://core.trac.wordpress.org/ticket/22624 where Andrew Nacin summarized it neatly:

Roles are merely a grouping of capabilities that can be assigned to a user. Only capabilities should be checked, not roles. If you need to know specifically what role a user has, there are a number of ways using the existing API.

A new function like this is ripe for abuse. We need people to check for capabilities, not roles, and having a current_user_has_role() function is just asking for them to do things wrong. We still haven't gotten people to stop checking for user levels, let's not go in this direction.

You use current_user_can('administrator') but also check out the notice in the current_user_can() docs:

While checking against particular roles in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results.

You can always write your own function wrappers or helpers to make your capability checks easier to work with.

1
  • 1
    Thanks for the info. I'll re-do some code and try my options. :) Dec 20 '21 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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