简体   繁体   English

如果用户未登录woocommerce则重定向

[英]redirect if user is not logged in woocommerce

I am creating a wordpress site for a client that wants to redirect certain products to login page. 我正在为想要将某些产品重定向到登录页面的客户端创建一个wordpress网站。 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. 思想将不胜感激。

Conceptually, you are on the right track with the second snippet. 从概念上讲,您在第二个片段的正确位置上。

  • You MUST use exit(); 您必须使用exit(); after a wp_redirect() call 在wp_redirect()调用之后

However, looking at the Woocommerce documentation, is_product() does not accept any arguments. 但是,查看Woocommerce文档, is_product()不接受任何参数。 This means passing '7854' in is_product(7854) is meaningless to the function. 这意味着在is_product(7854)传递'7854'对函数没有意义。 It returns a boolean (true|false) based on whether you are viewing a single product, regardless of which product. 根据您是否查看单个产品,无论哪个产品,它都会返回一个布尔值(true | false)。

Looking at the documentation for is_product_tag() I think that if you substituted is_product_tag( 'your-product-slug' ) that would work. 查看is_product_tag()的文档,我认为如果替换为is_product_tag( 'your-product-slug' )会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM