简体   繁体   English

检查在 Woocommerce 结帐期间注册的用户

[英]Check user registered during Woocommerce checkout

I need to track user registration during the checkout (eg.: include tracking code on Thank You page).我需要在结帐期间跟踪用户注册(例如:在感谢页面上包含跟踪代码)。 When a user registers on the checkout page there is no clue about the registration inside the $customer or $order object as far as I know.据我所知,当用户在结帐页面上注册时,没有任何关于$customer$order对象内部注册的线索。 Also, there is no query var to represent this.此外,没有查询变量来表示这一点。

How can I find out that a registration happened in the checkourt process?我怎样才能知道checkourt过程中发生了注册? As an alternative, I'd be glad about any alternative tracking option for this.作为替代方案,我很高兴为此提供任何替代跟踪选项。

I try to use the woocommerce_thankyou hook to check this.我尝试使用woocommerce_thankyou挂钩来检查这一点。 Additionally, I am aware that the user logs in after the checkout in this case.此外,我知道在这种情况下用户在结帐后登录。

My own solution is not the most proper way but works.我自己的解决方案不是最合适的方法,但有效。
Because registration during the checkout automatically logs in the user I can compare the registration date with the ThankYou page generation time.因为在结帐时注册会自动登录用户,所以我可以将注册日期与ThankYou 页面生成时间进行比较。 In this case If the user registered in the last 30 seconds the tracking code will be placed on the thank you page.在这种情况下,如果用户在过去 30 秒内注册,则跟踪代码将放置在感谢页面上。

add_action( 'woocommerce_thankyou', 'add_tracking_code_to_thankyou', 10, 1 );
function add_tracking_code_to_thankyou( $order_id ) {
    // Only for logged in users
    if ( $order_id && is_user_logged_in() ) {
        $udata = wp_get_current_user();
        $registered = new \DateTime($udata->user_registered);
        $current = new \DateTime();

        // get seconds elapsed after user registration
        $interval = $current->format('U') - $registered->format('U');

        if ($interval <= 30) {
            // echo 'tracking code';
        }
    }
}

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

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