7

After much research on the net, I have not found a solution that works.

Do you know how can I do that on woocommerce :

I try that for the moment :

function cm_redirect_users_by_role() {

    $current_user   = wp_get_current_user();
    $role_name      = $current_user->roles[0];

    if ( is_products_page() && is_single() ){
        if ( $role_name === 'customer' ) {
            wp_redirect( 'https://www.mysite.fr/shop/' );
        } // if
        else {
            wp_redirect( 'https://www.mysite.fr/' );
        } //
    }
} // cm_redirect_users_by_role
add_action( 'admin_init', 'cm_redirect_users_by_role' );

But that not work :/ when I'm on a single product page, I'm not redirect.

6

Here is the result that works, I put in the condition : is_product() to check if is a single product page. And I changed in the add_action() : admin_init by wp

function cm_redirect_users_by_role() {

    $current_user   = wp_get_current_user();
    $role_name      = $current_user->roles[0];

    if ( is_product() ){
        if ( $role_name !== 'customer' && $role_name !== 'shop_manager' && $role_name !== 'dc_vendor') {
            wp_redirect( 'https://www.mysite.fr/' );
        } // if
    }
} // cm_redirect_users_by_role
add_action( 'wp', 'cm_redirect_users_by_role' );
| improve this answer | |
  • Hi, Please consider using the same account for posting, commenting, answering, etc. – Jack Johansson Jun 13 '17 at 23:54

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.