WooCommerce电子邮件通知:不同城市的不同电子邮件收件人 [英] WooCommerce email notifications: different email recipient for different cities

查看:218
本文介绍了WooCommerce电子邮件通知:不同城市的不同电子邮件收件人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Woocommerce,实际上我只收到一封电子邮件的订单通知.我希望通过2封不同的电子邮件接收有关订单的通知,具体取决于客户的位置:

I using Woocommerce and actually I receive order notifications only to one email. I would like to receive notifications about orders in 2 different emails depending on customer location:

  • 对于来自1区(德国)的客户,我想在
    Mail #1 (mail1@mail.com)
  • 接收电子邮件通知
  • 对于其他所有区域,例如第2区(墨西哥),我希望在
    Mail #2 (mail2@mail.com) 接收电子邮件通知.
  • For customer from zone 1 (Germany) I would like to receive the email notifications at
    Mail #1 (mail1@mail.com),
  • For all other zones like zone 2 (Mexico) I would like to receive the email notifications at
    Mail #2 (mail2@mail.com).

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

I looking for some functions on net, but I was find only funtcions to send to two email adresses, but without any If condition.

我需要的是这样的东西:

What I will need is something like that:

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

我可以使用哪个钩子来使它正常工作?

Which hook can I use to get this working?

谢谢.

推荐答案

您可以使用挂钩在 woocommerce_email_recipient_{$this->id} 过滤器挂钩中的自定义函数,定位为新订单" 电子邮件通知,通过这种方式:

You can use a custom function hooked in woocommerce_email_recipient_{$this->id} filter hook, targeting 'New Order' email notification, this way:

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实例对象一起使用:

For WooCommerce 3+, some new methods are required and available from WC_Order class concerning billing country and shipping country: get_billing_country() and get_shipping_country()
Usage with $order instance object:

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

代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

代码已经过测试并且可以正常工作.

Code is tested and works.

相关答案:

  • How to get order ID in woocommerce_email_headers hook
  • Send on-hold order status email notification to admin

这篇关于WooCommerce电子邮件通知:不同城市的不同电子邮件收件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆