【问题标题】:Customizing email headers in WooCommerce email notifications在 WooCommerce 电子邮件通知中自定义电子邮件标题
【发布时间】:2018-01-07 10:53:32
【问题描述】:

我正在尝试编写一个过滤器来更改 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 <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);

这是 WooCommerce 高级通知插件中的代码。

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

进度编辑

所以现在基于 Loic 的善意建议,我已经应用了他的代码(如下),但它在启用 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 

【问题讨论】:

    标签: php wordpress woocommerce email-headers email-notifications


    【解决方案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 <manchester@city.org>';
        }
        else
        {
            $headers[] = 'Reply-to: London <london@city.org>';
        }
        return $headers;
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    此代码在 WooCommerce 版本 3+ 上经过测试并且可以正常工作。

    【讨论】:

    • 我已经测试过了,不幸的是它不适用于 WooCommerce 高级通知 - 我已经在上面的编辑中解释了原因。还有什么想法吗?!谢谢洛伊克。
    • @LinzDarlington 对不起……你应该在这个插件源代码中找到一个过滤钩子,因为它似乎没有使用经典的 woocommerce 核心功能或方法……
    • @LoicTheAztec 你能帮我解决这个问题吗stackoverflow.com/q/51558359/8913606
    • @melvin 你让我帮助你的代码……我有答案,因为你的代码不可测试。
    • @melvin 那么由于这个机密,我无法提供正确的答案(那么,下次如果不清楚和可测试,最好不要问我)请记住:“寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误以及重现它所需的最短代码问题本身。"*.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 2018-07-20
    • 2018-01-20
    • 2017-05-19
    • 2018-09-19
    相关资源
    最近更新 更多