Woocommerce:检查成功购买是来自新客户还是回头客 [英] Woocommerce: Checking if successful purchase was from a new or returning customer
本文介绍了Woocommerce:检查成功购买是来自新客户还是回头客的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有任何方法可以检查成功购买的是新客户还是回头客.
I am wondering if there is any way to check if a successful purchase was from a new or returning customer.
我有一个脚本需要添加到订单成功"页面.
I have a script which needs added to the Order Success page.
到目前为止我已经有了这个,它并没有真正按照我的需要工作,因为它只是检查访客或登录结帐:
I've got this so far, which doesn't really work as I need it to as it is only checking for guest or logged-in checkout:
$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.';
}
谢谢!
推荐答案
以下代码应该适用于回头客和新客户,而不管帐单电子邮件地址发生变化.这也适用于结帐时注册的新客户.
The following code should work for returning customer as well as new customer irrespective of a change in billing email address. This should also work for a new customer registering while checking out.
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
}
}
}
这篇关于Woocommerce:检查成功购买是来自新客户还是回头客的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文