Menu Close

WooCommerce: Bulk Re-Send All Customer’s Completed Order Emails

default

This is quite an uncommon WooCommerce requirement, but especially for downloadable products the Completed Order email is such as important one – and customers may end up asking you to resend them all their past order emails.

This is an interesting snippet, as it features important functionalities: getting orders by billing email, looping through the results and re-triggering the Completed Order email whenever a specific admin URL parameter is posted. Enjoy!

A customer requested to get all their Completed Order emails once again in their inbox. Maybe because they contain useful info, downloads, or they need to collect some data all together. Great – use their billing email address in any WordPress dashboard page, append it to the current URL with parameter ‘cust-email’, add the snippet below to your functions.php, and all emails will magically bulk trigger!

PHP Snippet: Trigger All Customer’s Past Order Emails @ WooCommerce Admin

While logged in as administrator and in the WordPress dashboard, add parameter “cust-email” with value = a customer’s billing email, hit enter, and the below snippet will trigger.

Example: test.com/wp-admin/post.php?post=232480&action=edit&cust-email=info@businessbloomer.com

/**
 * @snippet       Re-Send All Customer's Completed Emails
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'admin_init', 'bbloomer_trigger_customer_order_emails_admin' );
  
function bbloomer_trigger_customer_order_emails_admin() {
   if ( isset( $_REQUEST['cust-email'] ) && ! empty( $_REQUEST['cust-email'] ) ) {
      if ( ! current_user_can( 'manage_woocommerce' ) ) {
         wp_die( 'You do not have permission to bulk edit products' );
      }
      $email = sanitize_email( $_REQUEST['cust-email'] );
		if ( is_email( $email ) && email_exists( $email ) ) {
         global $wpdb;
			$result = $wpdb->get_col( $wpdb->prepare(
				"SELECT p.ID FROM {$wpdb->posts} AS p
				INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id
				WHERE p.post_status = 'wc-completed'
				AND pm.meta_key = '_billing_email'
				AND pm.meta_value = %s",
				$email
			));			
			$result = array_map( 'absint', $result );
			if ( $result ) {
				foreach ( $result as $order_id ) {
					$order = wc_get_order( $order_id );
					WC()->mailer()->emails['WC_Email_Customer_Completed_Order']->trigger( $order_id, $order );
				}
			} 
		}
	}
}
View Source
Posted in WooCommerce Tips