I’m trying to add in a feature where an admin order note will be added for all first time customers.
The code I am trying is:
add_action('woocommerce_thankyou', 'is_returning_customer', 10, 1);
function is_returning_customer($order_id)
{
if (!$order_id) {
return;
}
if(is_user_logged_in()) {
$order_status = array('wc-on-hold,','wc-processing', 'wc-completed');
$customer_id = get_current_user_id();
$customer_orders=get_posts( array(
'meta_key' => '_customer_user',
'meta_value' => $customer_id,
'post_type' => 'shop_order',
'post_status' => $order_status,
'numberposts' => -1
)
);
}
if (count($customer_orders) => 1) {
$order = wc_get_order( $order_id );
$note = '*** New Customer ***';
$order->add_order_note($note);
$order->save();
}
}
However, the problem it’s adding the new customer note on every order. Any advice?
2
Answers
Your code looks fine except for syntax errors here. I revised your code. try the below code.
Changed
To
Changed
To
for check customer is first time trying below condition.
for check customer is a return. try the below condition.
Complete code. for returning customer
Complete code for first time customer
Actually it is not necessary to go through the orders, as there is already a function for this in WooCommerce, namely wc_get_customer_order_count() – Get total orders by customer.
Only the issue here is, just like with your current code, that this would only work for logged in users.
To make this also work for ‘guests’ you can use the following:
Related: How to add a first order message after the buyer name in WooCommerce admin order list