【问题标题】:Redirect if the user didn't bought an item如果用户没有购买商品,则重定向
【发布时间】:2021-03-30 02:36:12
【问题描述】:

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

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

这是我正在尝试的:Reference code

这是我的代码。

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);

【问题讨论】:

  • 奥马尔,您似乎没有正确添加链接,只有参考文本进入您的问题,请编辑您的问题并正确重新应用参考代码链接,确保添加了链接本身。
  • 好的,我会为你添加到你的问题中。
  • 请编辑您的问题并将任何文本编辑应用到您的链接内容;)只需点击问题底部的编辑按钮,然后添加或编辑我添加的文本/链接。

标签: php woocommerce product shortcode


【解决方案1】:

请注意,这只是客户端,而不是服务器端:
您可以尝试将wp_redirect('https://learndigital-mena/my-account/'); 替换为echo '<script>window.location="https://learndigital-mena/my-account/"</script>';header("Location: https://learndigital-mena/my-account/"),但标题位置将位于脚本顶部。

【讨论】:

  • 感谢您的评论,很遗憾没有奏效。如果它确实有效,我认为如果我的用户禁用 JavaScript 可能会导致问题。
猜你喜欢
  • 2019-08-16
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 2011-06-08
  • 1970-01-01
  • 2023-03-25
  • 2021-03-31
  • 2014-11-05
相关资源
最近更新 更多