Question

I am creating a wordpress site for a client that wants to redirect certain products to login page. I have already had success redirecting entire shop page, etc.. but, I need to redirect only certain products....

This is what I have tried, but it redirects EVERY product to login page:

if( !is_user_logged_in() && $product_id = 7854 ) {
    wp_redirect('my-site-url');
}

I have also tried :

if( !is_user_logged_in() && is_product(7854))  {
    wp_redirect('my-site-url');
    exit;
}

and again, it redirects ALL products to login page.

Am I missing something here? Thoughts would be greatly appreciated.

Was it helpful?

Solution

Conceptually, you are on the right track with the second snippet.

  • You MUST use exit(); after a wp_redirect() call

However, looking at the Woocommerce documentation, is_product() does not accept any arguments. This means passing '7854' in is_product(7854) is meaningless to the function. It returns a boolean (true|false) based on whether you are viewing a single product, regardless of which product.

Looking at the documentation for is_product_tag() I think that if you substituted is_product_tag( 'your-product-slug' ) that would work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top