首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将购买特定产品的客户从单一产品页面重定向。

将购买特定产品的客户从单一产品页面重定向。
EN

Stack Overflow用户
提问于 2020-04-29 09:15:37
回答 1查看 170关注 0票数 2

我试图重定向顾客,他们购买的产品的ID为"12514“。

我的问题是:

我可以在哪里设置产品ID,这样,如果用户已经购买了一次产品,那么只有在该页面上才能启动重定向(现在是在每个产品页面上)。

代码语言:javascript
复制
add_action( 'template_redirect', 'single_product_redirect_logged_in_purchased' );

function single_product_redirect_logged_in_purchased() { 
    if ( ! is_product() && ! is_user_logged_in() ) return;  
    $current_user = wp_get_current_user();
    $product_id = get_queried_object_id();
    if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id ) ) {
        wp_safe_redirect('/custom-url');
        exit;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-29 09:20:31

只需在if语句中添加额外的条件即可。

代码语言:javascript
复制
add_action( 'template_redirect', 'single_product_redirect_logged_in_purchased' );

function single_product_redirect_logged_in_purchased() { 
    if ( ! is_product() && ! is_user_logged_in() ) return;  
    $current_user = wp_get_current_user();
    $product_id = get_queried_object_id();
    if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id ) && $product_id == 12514 ) {
        wp_safe_redirect('/custom-url');
        exit;
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61498244

复制
相关文章

相似问题

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