I am developing an woocommerce website where user has purchased only one product. He/She should be redirected to that Product page automatically on its login. Hence if there are say 50customer I want them to be redirected to 50 different pages after Login
1 Answer
You can use the filter "login_redirect", in the function to create you'll need to verify the role of the user, check for the order and get product id in the order. Then you'll be able to get the permalink for this product and redirect the user.
A little example, as you told that they can purchase only one product, this can work,
add_filter( 'login_redirect', 'login_customer_redirect', 10, 3 );
function login_customer_redirect($redirect_to, $request, $user){
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
if ( in_array( 'administrator', $user->roles ) ) {
// redirect them to the default place
return $redirect_to;
}
//check for customers
elseif ( in_array( 'customer', $user->roles ) ) {
$user_order = new WC_Order($user->ID);
$items = $user_order->get_items();
foreach ($items as $item) {
$product_id = $item['product_id'];
}
$redirect_to = get_permalink($product_id);
return $redirect_to;
} else {
return home_url();
}
} else {
return $redirect_to;
}
}