jjzjj

php - WooCommerce 电子邮件通知 : different email recipient for different cities

coder 2024-05-02 原文

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

我可以使用哪个钩子(Hook)来让它工作?

谢谢。

最佳答案

您可以使用 Hook 在 woocommerce_email_recipient_{$this->id} 过滤器 Hook 中的自定义函数,针对 '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;
}

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 文件或任何插件文件。

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


相关回答:

关于php - WooCommerce 电子邮件通知 : different email recipient for different cities,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41940348/

有关php - WooCommerce 电子邮件通知 : different email recipient for different cities的更多相关文章

随机推荐