0

I created two custom button in woocommerce shop page for every products, and i'm trying to do visible this two buttons only for administrator when he/she login as an administrator.

enter image description here

add_action( 'woocommerce_after_shop_loop_item', 'product_visibility_button', 5 );
     
function product_visibility_button() {
    echo '<div>';
    echo '<a class="button" style="margin:10px">BTN1</a>';
    echo '<a class="button" style="margin:10px">BTN1</a>';
    echo '</div>';
}
1

you can use the following code to check if a user is logged in and has a valid role.

add_action( 'woocommerce_after_shop_loop_item', 'product_visibility_button', 5 );
function product_visibility_button() {

    if ( is_user_logged_in() ) {
        $user = wp_get_current_user();
        if ( in_array( 'administrator', (array) $user->roles ) ) {
            echo '<div>';
            echo '<a class="button" style="margin:10px">BTN1</a>';
            echo '<a class="button" style="margin:10px">BTN1</a>';
            echo '</div>';
        }
    }
}
1
  • That's great, many thanks Nov 13 at 9:15

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.