WooCommerce电子邮件通知:不同城市的不同电子邮件收件人

时间:2017-01-30 16:04:31

标签: php wordpress woocommerce orders email-notifications

我使用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')

我可以使用哪个钩子来实现这个功能?

感谢。

1 个答案:

答案 0 :(得分:4)

您可以使用隐藏在 woocommerce_email_recipient_{$this->id} 过滤器中的自定义功能,定位&#39;新订单&#39; 电子邮件通知,这样:< / p>

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文件中,或者放在任何插件文件中。

代码经过测试并有效。

相关答案:

相关问题