问题描述
我正在使用 Woocommerce CSV导出插件.我希望有一种方法可以检查客户是否是新客户,如果是新客户,可以为自定义meta-key
和 true
值写入订单元数据.
但是,如果用户不是New,则什么也不会发生.
I'm using Woocommerce CSV Export plugin. I will like to have a way to check if the customer is NEW and if it is, to write in order metadata for a custom meta-key
a true
value.
But if user is not New, nothing will happen.
我首先想到的是WP用户(user_registered)的创建日期.但是我认为有更好,更快的方法.换句话说,我怎么知道这是否是客户的第一订单...
I thought first to start with the creation date of the WP user (user_registered). But I think there is a better and faster way. In other words, how can I know if this is the first order of a client...
我的目标:如果该客户是第一次订购,请在导出CSV"中为该订单提供一个 TRUE
值.
My goal: If this customer is ordering for the first time, have a TRUE
value, for this order in the Export CSV.
然后我尝试了使用此答案代码没有成功.
Then I have tried to use this answer code without success.
我的问题:
我怎么能做到这一点?
My question:
How could I achieve this?
谢谢
推荐答案
基于 (我最近做了),有可能会添加一个元键/ wp_postmeta
数据库中表中的新客户优先订单"值.因此,我们将以这种方式更改该条件函数:
Based on this answer code (I have recently made), it's possible to have a function that will add a meta key/value in the database wp_postmeta
table for a New customer first order. So we will change a bit that conditional function this way:
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;
}
此代码在您的活动子主题或主题的function.php文件中,或在插件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文件中.
要为已定义的订单ID获得此值,您将使用此参数(最后一个参数表示它是一个字符串):
To get this this value for a defined order ID, you will use this (last argument means it's a string):
// 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;
所有代码均已通过测试并且可以正常工作.
参考
- 模板结构+通过主题覆盖模板
- 检查客户是否已经购买了商品在WooCommerce中
- 检查如果客户早些时候在WooCommerce中购买了特定产品
- WP代码参考-update_post_meta()函数
- WP代码参考-get_post_meta()函数
- Template Structure + Overriding Templates via a Theme
- Checking if customer has already bought something in WooCommerce
- Check if a customer has purchased a specific product earlier in WooCommerce
- WP Code Reference - update_post_meta() function
- WP Code Reference - get_post_meta() function
这篇关于为WooCommerce CSV导出插件添加自定义字段-对于客户第一订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!