I need to find that a particular customer has done business with that store previously.
To achieve that I need to find the number of orders by a given customer.
How can I achieve that?
I tried Googling, but did not find any solution.
I need to find that a particular customer has done business with that store previously.
To achieve that I need to find the number of orders by a given customer.
How can I achieve that?
I tried Googling, but did not find any solution.
Comments are closed.
Just drop in the user id and you’ll get your total number of orders:
To go a step further for my own purposes, I use this code to get the number of a customer’s non-cancelled orders since I don’t want to count failed order attempts:
Modifying @MarkPraschan Code, which works well for me without throwing any notice, as i got 2 notice about undefined variable $user_id and i’m not passing the code through a function. Using below code worked for me (which gets the number of order transaction minus canceled orders);
if you intend to display both completed and non completed orders, you will use the first two line of the above code, which is;
Tested and working on;
WP = v4.9.9
WC = v3.5.3
I know this is an old question, but thought I’d share my code/info anyways.
The customer will be connected to the order via the postmeta key
_customer_user
in thewp_postmeta
table.You can lookup all orders with the status completed and processing for a specific user ID with the following query, where
279
is the user ID:When translated into PHP code that can be used on any WP installation simply by placing the code at the bottom of your theme
functions.php
file.This example displays the total orders for a customer on the order page in the back-end of WordPress, directly below the customer selection/dropdown. For instance if you need to know if this is the first order a customer has placed/done you can display a message. Obviously you will want to change the order status filter to something that suits your needs. Doing a direct query like below is more efficient I believe.
If you need a list format of multiple customers/users, then you can use the
$wpdb->get_results()
instead of$wpdb->get_var()
and loop over the results (table rows).Found a way.