我想知道是否有办法检查成功购买是来自新客户还是回头客。
我有一个需要添加到订单成功页面的脚本。
到目前为止我已经得到了这个,因为我只需要检查来宾或登录结帐,因此无法正常工作:
$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.';
}
谢谢!
答案 0 :(得分:0)
你可以简单地使用带有woocommerce_thankyou
的wordpress is_user_logged_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())
相关问题:woocommerce php snippets for proceeded to checkout to know user is login or not
<小时/> 更新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文件中。
代码经过测试并有效。
希望这有帮助!
答案 1 :(得分:0)
以下代码适用于回头客和新客户,无论帐单邮箱地址有何变化。这也适用于新客户在结账时注册。
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
}
}
}