Woocommerce after login redirect to admin dashboard for customer role

Viewed 540

I am using this code to redirect users with roles customer to after woocommerce login. (code from woocommerce after login redirect)

function wc_custom_user_redirect( $redirect, $user ) {
// Get the first of all the roles assigned to the user
$role = $user->roles[0];
$dashboard = admin_url();
$myaccount = get_permalink( wc_get_page_id( 'shop' ) );
if( $role == 'administrator' ) {
//Redirect administrators to the dashboard
$redirect = $dashboard;
} elseif ( $role == 'shop-manager' ) {
//Redirect shop managers to the dashboard
$redirect = $dashboard;
} elseif ( $role == 'editor' ) {
//Redirect editors to the dashboard
$redirect = $dashboard;
} elseif ( $role == 'author' ) {
//Redirect authors to the dashboard
$redirect = $dashboard;
} elseif ( $role == 'customer' || $role == 'subscriber' ) {
//Redirect customers and subscribers to the "My Account" page
$redirect = $dashboard;
} else {
//Redirect any other role to the previous visited page or, if not available, to the home
$redirect = wp_get_referer() ? wp_get_referer() : home_url();
}
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 ); 

For customer role, it doesn't redirect to dashboard page, instead of this it takes to default my-account page of woo. If I put any page of frontend for redirection e.g."shop","cart" then it redirects properly but it doesn't allow customer/subscriber to access the wordpress admin area. I have tried to create a new role with same capabilities as customer but it also doesn't allow access to dashboard (admin area). I want to create some pages in admin area and give access to customer(or any new role) there only.

Please help me.

0 Answers
Related