0

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

2
  • How will you get the product for each user. From their orders ? Random ?
    – Benoti
    Commented Oct 19, 2016 at 10:55
  • From their orders..
    – viki
    Commented Oct 20, 2016 at 15:28

1 Answer 1

0

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;
  }
}
2
  • Thanks you so much. Modified a lill n it worked like a charm
    – viki
    Commented Dec 20, 2016 at 11:32
  • Great, don't forget to mark your question as resolved !
    – Benoti
    Commented Dec 20, 2016 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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