In our webshop we have an invoice generation plugin. This plugin generates invoices when an order is set to complete. We send two mails. But due to that the plugin only generates an invoice on order completion only the "buyer" email contains the invoice
- Mail to webshop admin (Invoice not attached).
- Mail to buyer (Invoice attached).
I've been following this stackoverflow message. I've also added the filter described here, so that the program knows it must resend the email.
When I test this three mails are sent.
- Mail to webshop admin (Invoice not attached).
- Mail to buyer (Invoice attached).
- Mail to webshop admin (Invoice attached).
But i'm stuck on the first part, where the unhooking takes place. For some reason the first e-mail is still being sent. I've taken a look at the WC_Email_New_Order
class and I saw that there are a few more trigger hooks.
// Triggers for this email.
add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_pending_to_completed_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_failed_to_completed_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_failed_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_cancelled_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_cancelled_to_completed_notification', array( $this, 'trigger' ), 10, 2 );
add_action( 'woocommerce_order_status_cancelled_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 );
So I've changed my code a bit, but the first e-mail is still being sent.
/**
* Unhook and remove WooCommerce all default "New Order" emails.
*/
function unhook_those_pesky_emails($email_class)
{
remove_action('woocommerce_order_status_pending_to_processing_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
remove_action('woocommerce_order_status_pending_to_completed_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
remove_action('woocommerce_order_status_pending_to_on-hold_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
remove_action('woocommerce_order_status_failed_to_processing_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
remove_action('woocommerce_order_status_failed_to_completed_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
remove_action('woocommerce_order_status_failed_to_on-hold_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
remove_action('woocommerce_order_status_cancelled_to_processing_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
remove_action('woocommerce_order_status_cancelled_to_completed_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
remove_action('woocommerce_order_status_cancelled_to_on-hold_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger'));
}
add_action('woocommerce_email', 'unhook_those_pesky_emails');
/**
* trigger "New Order" email on "processing" status
*/
function process_new_order_notification($order_id, $order)
{
WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger($order_id);
}
add_action('woocommerce_order_status_completed', 'process_new_order_notification', 20, 2);
add_filter('woocommerce_new_order_email_allows_resend', '__return_true');
I was hoping someone could advice me here how I can get rid of the first e-mail sending iteration for admin.