I'm trying to write a simple plugin that would show new or returning customer information and the number of completed orders from the user. To do this, I created a folder called returning-customers.php and file with the same name. In this file, I wrote the next code:
/*
* Plugin Name: Kasha returning customers
* Description: Displays information returning customer or new, and also displays the number of completed orders
* Version: 1.0.0
* Author: Me
* License: GPLv2 or later
*/
add_filter( 'woocommerce_admin_order_data_after_order_details', 'render_customer_is_new_field' );
function render_customer_is_new_field($order)
{
$args = array(
'status' => 'completed',
'return' => 'ids',
);
if ( $order->get_user_id() !== 0 ) {
$args['customer_id'] = $order->get_customer_id();
} else {
$args['customer'] = $order->get_billing_email();
}
$customer_has_existing_orders = wc_get_orders( $args );
$orders_count = count($customer_has_existing_orders);
$markData = ( (int)count($customer_has_existing_orders) >= 2 )
? array('status-processing', 'Returning customer')
: array('status-on-hold', 'New Customer');
echo '
<p class="kasha-customer-counter" style="float: left">
<mark class="order-status '. $markData[0] .'">
<span>'. $markData[1] .' <strong style="color:#ff0000;font-weight: 900;"> '. $orders_count .'</strong></span>
</mark>
</p>
';
}
But this code does not work correctly. I always see that the customer has returned and all his orders are considered. However, the mark that the user is returning customer should be placed only after two orders with the status completed and only orders with the status completed should be considered. Now it looks like the wc_get_orders function is not working. When I write: var_dump(wc_get_orders()) I see NULL.