I Would like to figure out which of my Woo Commerce customers have not ordered from me in x days and send them a reminder email about something store related.
I would like to be able to do a user meta query to grab all inactive users.
Trying to avoid having to do the more expensive method which is grabbing all orders between a range, and then comparing them with recent orders to see which users aren’t in the recent orders using this query…
$orders_query = array(
'fields' => 'ids',
'post_type' => 'shop_order',
'post_status' => 'wc-completed',
'nopaging' => true,
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_completed_date',
'value' => $fromDate,
'compare' => '>=',
'type' => 'DATETIME'
),
array(
'key' => '_completed_date',
'value' => $toDate,
'compare' => '<=',
'type' => 'DATETIME'
)
) //AND
);
$result = get_posts($orders_query);
Does Woo Commerce add any metadata for the user for the last purchase?
I couldn’t find anything in documentation/database digging.
2
Answers
Woocommerce does save a meta value for a user’s last active time. Its a timestamp – meta key is “wc_last_active”. As far as I can tell through the code – it gets updated whenever a user logs in or one of their orders is updated (including being placed). I’m not sure if that will work for your purposes?
If what you’re worried about is efficiency of this query, you could also save this meta yourself? On placing an order you could simply add your own meta value for last order. That would make it much easier when you need to pull the data for display.
Hope that helps!
late to the party but for future Google-er
today I had to check what is saved in users meta and found out this peace of info:
_last_order
and some others are in meta .. and to get the date you simple do something like this:🙂 hope this help