基于this answer code(我最近做),它可能有一个将在数据库中添加元的键/值wp_postmeta
表为新客户第一顺序的功能。因此,我们将改变一个位是有条件的功能是这样的:
function new_customer_has_bought() {
$count = 0;
$new_customer = false;
// Get all customer orders
$customer_orders = get_posts(array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id()
));
// Going through each current customer orders
foreach ($customer_orders as $customer_order) {
$count++;
}
// return "true" when it is the first order for this customer
if ($count > 2) // or ($count == 1)
$new_customer = true;
return $new_customer;
}
此代码放在你的活跃儿童主题或主题,或插件php文件的function.php文件。
使用情况THANKYOU HOOK:
add_action('woocommerce_thankyou', 'tracking_new_customer');
function tracking_new_customer($order_id) {
// Exit if no Order ID
if (! $order_id) {
return;
}
// The paid orders are changed to "completed" status
$order = wc_get_order($order_id);
$order->update_status('completed');
// For 1st 'completed' costumer paid order status
if (new_customer_has_bought() && $order->has_status('completed'))
{
// Create 'first_order' custom field with 'true' value
update_post_meta($order_id, 'first_order', 'true'); needed)
}
else // For all other customer paid orders
{
// udpdate existing 'first_order' CF to '' value (empty)
update_post_meta($order_id, 'first_order', '');
}
}
此代码放在你的活跃儿童主题或主题的function.php文件,或者在一个插件PHP文件。
现在只为第一个新客户订单你将有一个自定义元数据,关键是'_first_customer_order'
和值是真。
要得到一个确定的顺序ID这个这个值,你会使用这个(最后一个参数意味着它是一个字符串):
// Getting the value for a defined $order_id
$first_customer_order = get_post_meta($order_id, 'first_order', false);
// to display it
echo $first_customer_order;
所有的代码进行测试和作品。
参考
欢迎来到SO! SO不是“我需要这个;给我代码”服务。那里有搜索引擎。请提供一些您已经尝试/遇到问题的代码,以便获得答案。 –