我需要找到一个特定的客户以前与该商店做过生意。
为了实现这一点,我需要找到给定客户的订单数量。
我怎样才能做到这一点?
我尝试了谷歌搜索,但没有找到任何解决方案。
答案 0 :(得分:1)
只需输入用户ID,您就会获得订单总数:
$numorders = wc_get_customer_order_count( $userid );
为了进一步实现自己的目的,我使用此代码获取客户的未取消订单的数量,因为我不想计算失败的订单尝试次数:
// Get TOTAL number of orders for customer
$numorders = wc_get_customer_order_count( $userid );
// Get CANCELLED orders for customer
$args = array(
'customer_id' => $userid,
'post_status' => 'cancelled',
'post_type' => 'shop_order',
'return' => 'ids',
);
$numorders_cancelled = 0;
$numorders_cancelled = count( wc_get_orders( $args ) ); // count the array of orders
// NON-CANCELLED equals TOTAL minus CANCELLED
$num_not_cancelled = $numorders - $numorders_cancelled;
答案 1 :(得分:0)
找到了一条路。
$args = [
'author' => 'id',
'post_status' => 'any',
'post_type' => 'shop_order'
];
$query = new WP_Query($args);
$orderCountByCustomer = $query->found_posts;
答案 2 :(得分:0)
修改@MarkPraschan代码,对我来说效果很好,而不会引起任何通知,因为我收到2条有关未定义变量$ user_id的通知,并且我没有通过函数传递代码。使用下面的代码对我有用(获得订单交易的数量减去已取消的订单);
$current_user = wp_get_current_user();
$numorders = wc_get_customer_order_count( $current_user->ID );
// Get CANCELLED orders for customer
$args = array(
'customer_id' => $current_user->ID,
'post_status' => 'cancelled',
'post_type' => 'shop_order',
'return' => 'ids',
);
$numorders_cancelled = 0;
$numorders_cancelled = count( wc_get_orders( $args ) ); // count the array of orders
// NON-CANCELLED equals TOTAL minus CANCELLED
$num_not_cancelled = $numorders - $numorders_cancelled;
如果您打算同时显示已完成和未完成的订单,则将使用上述代码的前两行;
$current_user = wp_get_current_user();
$numorders = wc_get_customer_order_count( $current_user->ID );
经过测试并正在研究;
WP = v4.9.9
WC = v3.5.3