Send a daily order status email to WooCommerce store owner. This is primarily to highlight orders that may need attention.
A new client is having ongoing problems related to the payment gateway – sometimes an order is paid but it is displayed as cancelled and no order emails are sent. This post is not about that but about creating a nightly email with a list of orders created the previous day. The store owner no longer has to login and check for pending or cancelled orders – the daily email will let them know.
Where to start
On my local test installation I opened the WooCommerce/Orders page and searched for the ‘Change status to processing‘ text from the ‘Bulk Actions‘ dropdown and found it in includes/admin/list-tables/class-wc-admin-list-table-orders.php. This demonstrated a number of the member functions for the WC_Order class which was helpful later on.
While considering using WP_Query to search for orders I found a very helpful page on retrieving orders: wc_get_orders and WC_Order_Query. It demonstrated retrieving orders based on order creation date. (I even got to fix some small errors in a bit of sample code)
Group by status
I chose to use
wc_get_orders() as the code was simple and it used less queries than the equivalent WC_Order_Query() code. After that I just go through each order and store the summary in a multi-dimensional array (grouping by status).
Then I create the email body, adding the orders grouped by status. I list Cancelled and Pending Payment orders first as those statuses are most important and most likely to need attention from the store owner. The rest of the statuses are added under those.
The script can be run nightly by cron either on your server or via a web based service (I use cron-job.org).