Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active October 3, 2021 15:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/528345d342fd70f8273bbb640d1b7a8d to your computer and use it in GitHub Desktop.
Save damiencarbery/528345d342fd70f8273bbb640d1b7a8d to your computer and use it in GitHub Desktop.
Daily WooCommerce Order Report - Send a daily order status email to WooCommerce store owner. This is primarily to highlight orders that may need attention. https://www.damiencarbery.com/2018/02/daily-woocommerce-order-report
<?php
define('WP_USE_THEMES', false);
/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-load.php' );
function dcwd_status_set_html_content_type() {
return 'text/html';
}
$yesterday = date( 'Y-m-d', strtotime( '-1 days' ) );
//$yesterday = date( 'Y-m-d' ); // DEBUG
$args = array(
'date_created' => $yesterday,
'limit' => -1, // Retrieve *all* of yesterday's orders.
);
$orders = wc_get_orders( $args );
// Skip over if no orders.
if ( count( $orders ) ) {
$orders_by_status = array();
foreach ( $orders as $order ) {
// Skip refund orders as they will be discovered via the original order's refunded status.
if ( 'shop_order_refund' != $order->get_type() ) {
$orders_by_status[ $order->get_status() ][] = sprintf( '<li><a href="%s">%s</a> %s %s for %s - %s</li>%s',
get_edit_post_link( $order->get_id() ), $order->get_id(),
$order->get_billing_first_name(), $order->get_billing_last_name(),
$order->get_formatted_order_total(), wc_get_order_status_name( $order->get_status() ), "\n" );
}
}
$subject = 'Orders received ' . date( 'l j F Y', strtotime( '-1 days' ) );
$mail_body = '<h1>' . $subject . "</h1>\n\n";
// List Cancelled and Pending orders first as they may require action.
$status = 'cancelled';
if ( array_key_exists( $status, $orders_by_status ) ) {
$mail_body .= '<h2>' . wc_get_order_status_name( $status ) . '</h2><ul>'. implode( '', $orders_by_status[ $status ] ) . '</ul>' . "\n";
}
$status = 'pending';
if ( array_key_exists( $status, $orders_by_status ) ) {
$mail_body .= '<h2>' . wc_get_order_status_name( $status ) . '</h2><ul>'. implode( '', $orders_by_status[ $status ] ) . '</ul>' . "\n";
}
$important_statuses = array( 'cancelled', 'pending' );
foreach ( array_keys( $orders_by_status ) as $status ) {
if ( ! in_array( $status, $important_statuses ) ) {
$mail_body .= '<h2>' . wc_get_order_status_name( $status ) . '</h2><ul>'. implode( '', $orders_by_status[ $status ] ) . '</ul>' . "\n";
}
}
add_filter( 'wp_mail_content_type', 'dcwd_status_set_html_content_type' );
$headers[] = 'From: '. get_bloginfo( 'name' ) .' <' . get_bloginfo( 'admin_email' ) . '>';
wp_mail( get_bloginfo( 'admin_email' ), $subject, $mail_body, $headers );
// Reset content-type to avoid conflicts -- http://core.trac.wordpress.org/ticket/23578
remove_filter( 'wp_mail_content_type', 'dcwd_status_set_html_content_type' );
}
@michaelhjulskov
Copy link

HI, where do you put this file? in which folder? Thank You

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment