更好的解决方案是使用woocommerce_order_status_cancelled
钩子函数,所以缺少$order
变量
也作为行动挂钩woocommerce_order_status_cancelled
当订单状态更改为“已取消”时触发,因此在您的代码中,if ($order->has_status(\'cancelled\')) {
不需要。
那么您的代码将是:
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 );
}
或者
woocommerce_order_status_changed
钩子(你的问题钩子):
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 );
}
}
代码进入函数。活动子主题(或活动主题)的php文件。已测试并正常工作。