2016-08-16 68 views
2

我正在使用Woocommerce CSV导出插件。 我想要有一种方法来检查客户是否是新的,如果是,写入订单元数据为定制meta-key a true
但是,如果用户不是新的,什么都不会发生。为WooCommerce CSV导出插件添加自定义字段 - 客户第一次订购

我首先想到了WP用户(user_registered)的创建日期。但我认为有一个更好更快的方法。换句话说,我怎么能知道这是一个客户端的一阶...

我的目标:如果该客户订购的第一次,有一个TRUE值,该顺序导出CSV。

然后我试了to use this answer code没有成功。

我的问题:
我怎么能做到这一点?

感谢

+1

欢迎来到SO! SO不是“我需要这个;给我代码”服务。那里有搜索引擎。请提供一些您已经尝试/遇到问题的代码,以便获得答案。 –

回答

2

基于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; 

所有的代码进行测试和作品。


参考

+0

明天我会测试。谢谢你分享。 –

相关问题