【问题标题】:WooCommerce email notifications: different email recipient for different citiesWooCommerce 电子邮件通知:不同城市的不同电子邮件收件人
【发布时间】:2017-06-15 20:56:23
【问题描述】:

我使用 Woocommerce,实际上我只收到一封电子邮件的订单通知。我想根据客户所在的位置通过 2 封不同的电子邮件接收有关订单的通知:

  • 对于来自 1 区(德国)的客户,我希望通过
    Mail #1 (mail1@mail.com) 接收电子邮件通知,
  • 对于所有其他区域,例如 2 区(墨西哥),我希望通过
    Mail #2 (mail2@mail.com) 接收电子邮件通知。

我在网上寻找一些功能,但我发现只有功能可以发送到两个电子邮件地址,但没有任何 If 条件。

我需要的是这样的:

if ($user->city == 'Germany') $email->send('mail1@mail.com')
else $email->send('mail2@mail.com')

我可以使用哪个钩子来完成这项工作?

谢谢。

【问题讨论】:

  • 你提供的代码有什么问题?
  • 这只是例子,我的意思
  • 您提供的示例看起来不错。那你在寻求什么帮助?您能否提供更多代码,以便更清楚地说明您遇到的问题?

标签: php wordpress woocommerce orders email-notifications


【解决方案1】:

您可以使用挂在 woocommerce_email_recipient_{$this->id} 过滤器挂钩中的自定义函数,以 'New Order' 电子邮件通知为目标,这样:

add_filter( 'woocommerce_email_recipient_new_order', 'diff_recipients_email_notifications', 10, 2 );
function diff_recipients_email_notifications( $recipient, $order ) {
    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;

    // Set HERE your email adresses
    $email_zone1 = 'name1@domain.com';
    $email_zone_others = 'name2@domain.com';

    // Set here your targeted country code for Zone 1
    $country_zone1 = 'GE'; // Germany country code here

    // User Country (We get the billing country if shipping country is not available)
    $user_country = $order->shipping_country;
    if(empty($user_shipping_country))
        $user_country = $order->billing_country;

    // Conditionaly send additional email based on billing customer city
    if ( $country_zone1 == $user_country )
        $recipient = $email_zone1;
    else
        $recipient = $email_zone_others;

    return $recipient;
}

对于 WooCommerce 3+,需要一些新方法,可从WC_Order 类中获得有关帐单国家和运输国家的一些新方法:get_billing_country()get_shipping_country() ...与 $order 实例对象一起使用

$order->get_billing_country(); // instead of $order->billing_country;
$order->get_shipping_country(); // instead of $order->shipping_country;

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

代码已经过测试并且可以工作。


相关答案:

【讨论】:

    猜你喜欢
    • 2019-05-30
    • 2017-06-11
    • 2019-08-28
    • 1970-01-01
    • 2018-09-14
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多