Skip to content

Instantly share code, notes, and snippets.

@ahmu83
Last active October 24, 2017 17:45
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 ahmu83/e6e8aa2febc1b89f9ecb2deff6e4616b to your computer and use it in GitHub Desktop.
Save ahmu83/e6e8aa2febc1b89f9ecb2deff6e4616b to your computer and use it in GitHub Desktop.
Send WooCommerce Order Emails Against an order id and email id
<?php
/**
* Send WooCommerce order email(s)
*
* @param integer $order_id WooCommerce order id.
* @param string|array $email_id The id of the email or an array of the email ids that need to send.
* @return bool Type returned Will return true if the email id provided is sent successfully.
*/
function send_wc_order_email($order_id, $email_id) {
// Email ids of all the order emails (for reference purpose)
$email_ids = array(
'new_order',
'cancelled_order',
'failed_order',
'customer_on_hold_order',
'customer_processing_order',
'customer_completed_order',
'customer_refunded_order',
'customer_invoice',
'customer_note',
'customer_reset_password',
'customer_new_account',
);
$mailer = WC()->mailer();
$emails = $mailer->get_emails();
$email_sent = false;
$emails_enabled = is_array($email_id) ? $email_id : array($email_id);
if ( ! empty( $emails ) ) {
foreach ( $emails as $email ) {
if (in_array($email->id, $emails_enabled)) {
$email->trigger($order_id);
$email_sent = true;
}
}
}
return $email_sent;
}
@ahmu83
Copy link
Author

ahmu83 commented Jul 25, 2017

In order to halt/block the default WooCommerce order emails use this: https://gist.github.com/mikejolley/3097073
Make sure you have enabled those emails you want to send in WooCommerce admin ( WC > Settings > Emails ) in order for the above function to work ( send_wc_order_email($order_id, $email_id) )

@DavidSteinbauer
Copy link

DavidSteinbauer commented Oct 24, 2017

@ahmu83 where do i hook the function send_wc_order_email in order to get it work?

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