douzhiling3166 2014-02-09 16:57 采纳率: 100%
浏览 79
已采纳

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

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.

  • 写回答

1条回答 默认 最新

  • douling8772 2014-02-10 02:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效