12

I want to be able to trigger the WooCommerce order complete email at a different stage in the WooCommerce checkout process. So I've disabled WooCommerce order complete email from the backend and am now looking for a line of code that will trigger the email at the point that I want. I've done a bit of research and I've found how to remove the order complete email but not how to trigger it manually. Any tips?

Thanks!

3 Answers 3

17

You can try this

$mailer = WC()->mailer();
$mails = $mailer->get_emails();
if ( ! empty( $mails ) ) {
    foreach ( $mails as $mail ) {
        if ( $mail->id == 'customer_completed_order' ) {
           $mail->trigger( $order->id );
        }
     }
}
1
  • Thank you. Any way to ensure someone doesn't abuse it, i.e. if I throw that into a specific place on the thankyou page, is there anyway to not send an email every time a person refreshes the page? Apr 20, 2015 at 3:51
8

Rather than looping or reusing the same object as suggested by @Sumit. You can initiate a new object and then call the trigger.

$email_oc = new WC_Email_Customer_Completed_Order();
$email_oc->trigger($order_id);
2
  • If this is a default WP order class then yes.. if it is custom it will be added using the woocommerce_email_classes filter, therefore the custom email class might just not be available on request. May 14, 2021 at 13:25
  • @RamonFincken this is in WooCommerce core
    – GeorgeP
    Feb 9 at 0:45
1

It can also be used like that;

WC()->mailer()->emails['WC_Email_Customer_Completed_Order']->trigger( $order_id );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.