-1

i have a custom Woocommerce link that adds a product to the cart and redirects user to the account page:

account/?add-to-cart=15169

I'd like this link to redirect only logged-in users to some specific URL and all the other users to the Account page

Right now:

Not Logged in users redirect to create an account page, that is fine.

Logged in users redirect to Account page

Question: Is there a way to redirect only logged-in users to a specific url for this specific link rather than the account page?

I have tried:

//redirect logged-in users from Account page id=90 to specific page id=15498
function my_logged_in_redirect() {

if ( is_user_logged_in() && is_page( 90 ) )
{
    wp_redirect( get_permalink( 15498 ) );
    die;
}

}
add_action( 'template_redirect', 'my_logged_in_redirect' );

that works but then the account page is actually not accessible from logged-in users.

1

you can check the query string and redirect if query string matches the product id.

function my_logged_in_redirect() {
    if ( is_user_logged_in() && is_page( 90 ) && $_GET['add-to-cart'] == 15169 ) {
        wp_redirect( get_permalink( 15498 ) );
        die;
    }
}
add_action( 'template_redirect', 'my_logged_in_redirect' );
  • you can accept this answer if it worked for you, so another user with same questions can use it as reference :) – Akshat Nov 28 '18 at 12:37

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.