2017-07-31 168 views
1

我正在尝试编写一个过滤器,用于更改WooCommerce电子邮件的回复地址,具体取决于订单所针对的“开票城市”。在WooCommerce电子邮件通知中自定义电子邮件标题

这适用于WooCommerce电子邮件,但似乎不适用于WooCommerce高级通知 - 它似乎没有将'$ order'传递到标头信息中以执行IF语句。

这是一个远射,但任何想法非常感谢。这是我的代码。

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 <[email protected]>'; 
return $headers; 
} else { 
    $headers = array(); 
    $headers[] = 'Reply-to: London <[email protected]>'; 
return $headers; 
} 

add_filter('woocommerce_email_headers', 'change_headers', 10, 3); 

这是WooCommerce Advanced Notifications插件中的代码。

/** 
* 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()); 
} 

进展编辑

基于卢瓦克的样的建议,所以我已申请了他的代码(见下文),但在启用WooCommerce高级声明它抛出这个致命错误。这是因为由于某种原因,'$ order'参数未正确传递到WooCommerce高级通知电子邮件的过滤器,所以$ city = $ order-> get_billing_city();是空的。

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 

回答

1

你已经倒在你的函数就woocommerce_email_headers过滤钩子的参数(注:我不使用WooCommerce高级通知插件)

相反试试这个(我有做一些变化不大):

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 <[email protected]>'; 
    } 
    else 
    { 
     $headers[] = 'Reply-to: London <[email protected]>'; 
    } 
    return $headers; 
} 

代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。

此代码对WooCommerce版本3+和工程测试。

+1

感谢LoicTheAztec。我会用明亮的眼睛来测试明天,然后回复。 –

+0

我已经测试过,不幸的是它不适用于WooCommerce高级通知 - 我已经解释了为什么在我上面编辑。任何进一步的想法?感谢Loic。 –

+1

@LinzDarlington对不起,你...你应该需要在这个插件源代码中找到一个过滤钩,因为它似乎不使用经典的woocommerce核心函数或方法... – LoicTheAztec