Last active
October 24, 2017 17:45
-
-
Save ahmu83/e6e8aa2febc1b89f9ecb2deff6e4616b to your computer and use it in GitHub Desktop.
Send WooCommerce Order Emails Against an order id and email id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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) )