A better solution will be to use all arguments included in woocommerce_order_status_cancelled
hooked function, so the missing $order
variable.
Also as the action hook woocommerce_order_status_cancelled
is triggered when order status change to “cancelled”, so in your code, if ($order->has_status('cancelled')) {
is not needed.
Then your code will be:
add_filter('woocommerce_order_status_cancelled', 'woocommerce_send_mail_abandonned_order', 10, 2 );
function woocommerce_send_mail_abandonned_order( $order_id, $order )
{
define("HTML_EMAIL_HEADERS", array('Content-Type: text/html; charset=UTF-8'));
// Get WooCommerce mailer from instance
$mailer = WC()->mailer();
ob_start();
include( 'email/mail.php' );
$message = ob_get_clean();
// Wrap message using woocommerce html email template
$wrapped_message = $mailer->wrap_message("Vous n'avez pas oublié quelque chose ?", $message);
// Create new WC_Email instance
$wc_email = new WC_Email;
// Style the wrapped message with woocommerce inline styles
$html_message = $wc_email->style_inline($wrapped_message);
// Send the email using wordpress mail function
wp_mail( $order->get_billing_email(), 'Oups Vous n\'avez pas oubliez quelque chose ?', $html_message, HTML_EMAIL_HEADERS );
}
Or with woocommerce_order_status_changed
hook (your question hook):
add_filter('woocommerce_order_status_changed', 'woocommerce_send_mail_abandonned_order', 10, 4 );
function woocommerce_send_mail_abandonned_order( $order_id, $old_status, $new_status, $order )
{
if ( $new_status === 'cancelled')
{
define("HTML_EMAIL_HEADERS", array('Content-Type: text/html; charset=UTF-8'));
// Get WooCommerce mailer from instance
$mailer = WC()->mailer();
ob_start();
include( 'email/mail.php' );
$message = ob_get_clean();
// Wrap message using woocommerce html email template
$wrapped_message = $mailer->wrap_message("Vous n'avez pas oublié quelque chose ?", $message);
// Create new WC_Email instance
$wc_email = new WC_Email;
// Style the wrapped message with woocommerce inline styles
$html_message = $wc_email->style_inline($wrapped_message);
// Send the email using wordpress mail function
wp_mail( $order->get_billing_email(), 'Oups Vous n\'avez pas oubliez quelque chose ?', $html_message, HTML_EMAIL_HEADERS );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.