首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果用户未购买商品,则重定向

如果用户未购买商品,则重定向
EN

Stack Overflow用户
提问于 2020-12-20 04:10:43
回答 1查看 40关注 0票数 0

这是到目前为止我得到的最好的结果,但我得到了很多错误。

从主要问题开始;它确实重定向到正确的url,但页面没有显示任何内容。

这就是我正在尝试的:Reference code

这是我的代码。

代码语言:javascript
复制
function pre_process_shortcode() {
  if (!is_singular()) return;
  global $post;
  $user = wp_get_current_user();
  $product_id = 27;
  $product_bought = wc_customer_bought_product( $user->user_email, $user->ID , $product_id );
  if (!empty($post->post_content)) {
    $regex = get_shortcode_regex();
    preg_match_all('/'.$regex.'/',$post->post_content,$matches);
    if (!empty($matches[2]) && in_array('protactPage',$matches[2]) && $product_bought && is_user_logged_in()) {
      wp_redirect('https://learndigital-mena.com/cart-2/');
        exit();
    } else {
      // login form or redirect to login page
         wp_redirect('https://learndigital-mena/my-account/');
        exit();
    }
  }
}
add_action('template_redirect','pre_process_shortcode',1);
EN

回答 1

Stack Overflow用户

发布于 2020-12-20 04:19:40

注意,这只是客户端,而不是服务器端:

您可以尝试将wp_redirect('https://learndigital-mena/my-account/');替换为echo '<script>window.location="https://learndigital-mena/my-account/"</script>';header("Location: https://learndigital-mena/my-account/"),但标头位置将位于脚本的顶部。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65373993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档