我正在使用Woocommerce CSV导出插件。我希望有一种方法来检查客户是否是新的,如果是的话,按照顺序为一个自定义的meta-key ( true value )编写元数据。
但是如果用户不是新用户,什么都不会发生。
我想首先从WP用户(user_registered)的创建日期开始。但我认为有一个更好、更快的方法。换句话说,我怎么知道这是客户的第一份订单.
我的目标是:如果这个客户是第一次订货,有一个TRUE值,这个订单在Export中。
然后我尝试了若要使用此应答代码,请执行以下操作,但没有成功。
我的问题:
我怎样才能做到这一点?
谢谢
发布于 2016-08-17 00:56:16
基于wp_postmeta (我最近做了),有可能在数据库这个答案代码表中为New订单添加一个元键/值。因此,我们将以这样的方式改变条件函数:
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文件中,或者在plugin文件中。
在谢谢钩子中的用法:
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文件中,或者在plugin文件中。
现在,
'_first_customer_order'只为FIRST new customer order使用,您将拥有一个自定义元数据,其中key为,值为trueE 221。
要获得定义的订单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;所有的代码都经过测试和工作。
参考文献
https://stackoverflow.com/questions/38984460
复制相似问题