Php Woocommerce:检查是否从新客户或退货客户处成功购买

Php Woocommerce:检查是否从新客户或退货客户处成功购买,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我想知道是否有任何方法可以检查一个成功的购买是从一个新的或返回的客户 我有一个脚本,需要添加到订单成功页面 到目前为止,我已经做到了这一点,但它并没有像我需要的那样真正起作用,因为它只是在检查客人或登录结账: $order = wc_get_order($order->id); $user = get_user_by('email', $order->billing_email); if (isset($user->ID)) { echo 'User is logge

我想知道是否有任何方法可以检查一个成功的购买是从一个新的或返回的客户

我有一个脚本,需要添加到订单成功页面

到目前为止,我已经做到了这一点,但它并没有像我需要的那样真正起作用,因为它只是在检查客人或登录结账:

$order = wc_get_order($order->id);
$user = get_user_by('email', $order->billing_email);

if (isset($user->ID)) {
    echo 'User is logged in.';
} else {
    echo 'User is a guest.';
}

谢谢

您可以简单地使用wordpress
is\u user\u logged\u in()
函数和钩子来检查订单状态和用户是否登录

add_action('woocommerce_thankyou', 'my_custom_tracking', 10, 1);

function my_custom_tracking($order_id) {
    if (!$order_id) {
        return;
    }
    // Lets grab the order
    $order = wc_get_order($order_id);

    $_billing_email = get_post_meta($order_id, '_billing_email', true);
    $user = get_user_by('email', $_billing_email);

    //for successful order
    if (in_array($order->status, ['processing', 'completed'])) {
        if (is_user_logged_in() || $user) {
            //it is a returning user
        } else {
            //user is a guest
        }
    }
    //unsuccessful order
    else {

    }
}
请注意:如果您只想检查用户是否登录,则将if(is_user_Logged_In()| |$user)替换为if(is_user_Logged_In())

相关问题:
更新了
v2

add_action('woocommerce_thankyou', 'wh_isReturningCustomer', 10, 1);

function wh_isReturningCustomer($order_id) {
    if (!$order_id) {
        return;
    }
    // Lets grab the order
    //$order = wc_get_order($order_id);

    $_billing_email = get_post_meta($order_id, '_billing_email', true);

    $args = [
        'post_type' => 'shop_order',
        'post__not_in' => [$order_id], //exclude current Order ID from order count
        'post_status' => ['wc-processing', 'wc-completed'],
        'posts_per_page' => -1,
        'meta_query' => [
            'relation' => 'AND',
            [
                'key' => '_billing_email',
                'value' => $_billing_email,
                'compare' => '=',
            ]
        ]
    ];
    $posts = new WP_Query($args);
    if ($posts->post_count) {
        //it is a returning user
    } else {
        //user is a guest
    }
}
代码进入活动子主题(或主题)的function.php文件。也可以在任何插件php文件中使用。
代码经过测试并正常工作


希望这有帮助

以下代码应适用于返回客户和新客户,无论账单电子邮件地址是否发生变化。这也适用于在签出时注册的新客户

add_action('woocommerce_thankyou', 'is_returning_customer', 10, 1);

function is_returning_customer($order_id) 
{
    if (!$order_id) {
        return;
    }
    if(is_user_logged_in()) {
        $order_status = array('wc-on-hold', 'wc-processing', 'wc-completed');
        $customer_id = get_current_user_id(); 
            $customer_orders=get_posts( array(
                'meta_key' => '_customer_user',
                'meta_value' => $customer_id,
                'post_type' => 'shop_order', 
                'post_status' => $order_status,
                'numberposts' => -1
            )
        );
        if(count($customer_orders)>1) {
            //returning customer
        } else {
            //new customer
        }
    }
}

谢谢你的帮助!我能找到的唯一问题是,如果用户在结账时注册并成功购买产品,他们将始终被视为“回头客”,因为他们将登录并找到他们的账单电子邮件。有办法吗?再次感谢。@stevie-c91:我已经更新了我的答案,请检查一下。使用
wh\u isReturningCustomer()
函数。非常感谢!你帮了大忙!不客气,我很高兴这对你有帮助。只有一点,如果用户是客人,并且它仍然有4-5个订单,那么您将无法获得
$user\u id
我对其进行了轻微定制,因此在我的情况下,客人将有一个id,新客户有一个不同的id,而返回的客户有另一个id。干杯:)