I am trying to write a filter that changes the reply-to address for WooCommerce email, dependent on the 'billing city' the order is made for.
This works for WooCommerce emails, but doesn't seem to work for WooCommerce Advanced notifications - it doesn't seem to pass the '$order' into the header information to do the IF statement.
It is a long shot but any thoughts much appreciated. This is my code.
function change_headers($order, $object, $headers);
// Get the city
$city = $order->get_billing_city();
// Change Headers
if ($city == 'Manchester') {
$headers = array();
$headers[] = 'Reply-to: Manchester <manchester@city.org>';
return $headers;
} else {
$headers = array();
$headers[] = 'Reply-to: London <london@city.org>';
return $headers;
}
add_filter( 'woocommerce_email_headers', 'change_headers', 10, 3);
This is the code in WooCommerce Advanced Notifications plugin.
/**
* sends an email through WooCommerce with the correct headers
*/
public function send( $id, $object, $is_plain_text, $to, $subject, $message, $notification ) {
$email = new WC_Email();
if ( $is_plain_text ) {
$message = wordwrap( strip_tags( $message ), 60 );
$email->email_type = 'plain';
} else {
$email->email_type = 'html';
}
$email->id = "advanced_notification_{$notification->notification_id}";
$email->send( $to, $subject, $message, $email->get_headers(), $email->get_attachments() );
}
PROGRESS EDIT
So now based on Loic's kind advice I've applied his code (below) but it throws this Fatal error with WooCommerce Advanced Notifications enabled. This is because for some reason the '$order' argument is not correctly passed through to the filter on WooCommerce Advanced Notifications emails so $city = $order->get_billing_city(); is empty.
Fatal error: Uncaught Error: Call to a member function get_billing_city() on null in /home/benefacto/public_html/dev/wp-content/plugins/bnfo-custom-emails/bnfo-custom-emails.php:130 Stack trace: #0 /home/benefacto/public_html/dev/wp-includes/class-wp-hook.php(298): custom_email_notification_headers('Content-Type: t...', 'advanced_notifi...', NULL) #1 /home/benefacto/public_html/dev/wp-includes/plugin.php(203): WP_Hook->apply_filters('Content-Type: t...', Array) #2 /home/benefacto/public_html/dev/wp-content/plugins/woocommerce/includes/emails/class-wc-email.php(287): apply_filters('woocommerce_ema...', 'Content-Type: t...', 'advanced_notifi...', NULL) #3 /home/benefacto/public_html/dev/wp-content/plugins/woocommerce-advanced-notifications/includes/class-wc-advanced-notifications.php(257): WC_Email->get_headers(NULL) #4 /home/benefacto/public_html/dev/wp-content/plugins/woocommerce-advanced-notifications/includes/class-wc-advanced-notifications.php(319): WC_Advanced_Notifications->send('new_order', Object(WC_Order), '0', 'l in /home/benefacto/public_html/dev/wp-content/plugins/bnfo-custom-emails/bnfo-custom-emails.php on line 130
You have inverted the arguments in your function regarding woocommerce_email_headers
filter hook (Note: I don't use WooCommerce Advanced Notifications plugin).
Instead try this (I have make some little changes):
add_filter( 'woocommerce_email_headers', 'custom_email_notification_headers', 10, 3 );
function custom_email_notification_headers( $headers, $email_id, $order ) {
// Get the city
$city = $order->get_billing_city();
$headers = array( $headers );
if ($city == 'Manchester')
{
$headers[] = 'Reply-to: Manchester <manchester@city.org>';
}
else
{
$headers[] = 'Reply-to: London <london@city.org>';
}
return $headers;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested on WooCommerce versions 3+ and works.
来源:https://stackoverflow.com/questions/45423138/customizing-email-headers-in-woocommerce-email-notifications