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

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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 7
 * @community     https://businessbloomer.com/club/
 */

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 );
				}
			} 
		}
	}
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *