简体   繁体   English

Woocommerce - 需要根据邮政编码向特定地址发送电子邮件

[英]Woocommerce - Need to send email to specific address based on zip code

Basically, in woocommerce you have the option to input multiple email addresses (separated by commas) of who to send the completed order to, in WooCommerce -> Settings -> Emails -> New order.基本上,在 woocommerce 中,您可以选择在 WooCommerce -> 设置 -> 电子邮件 -> 新订单中输入多个电子邮件地址(用逗号分隔)将完成的订单发送给谁。 But I need a way to send to only one of these recipients based on the zip code of the customer who is ordering the product.但我需要一种方法,根据订购产品的客户的邮政编码仅发送给这些收件人中的一个。 Or completely overwrite woocommerce's way of handling this.或者完全覆盖woocommerce的处理方式。

How can I tie into the function responsible for this, in order to send to the correct recipient?为了发送给正确的收件人,我如何才能与负责此操作的功能联系起来? Basically, is there a hook defined for this, or does a plugin exist for something like this, or will I have to edit core WooCommerce files?基本上,是否为此定义了一个钩子,或者是否存在这样的插件,或者我是否必须编辑核心 WooCommerce 文件? If edits are needed to core files, can someone point me in the right direction as to which files will need edits?如果需要对核心文件进行编辑,有人可以指出我需要编辑哪些文件的正确方向吗?

I had a bit of trouble with helgatheviking's answer above and also a slightly different use case.我在上面的 helgatheviking 的回答中遇到了一些麻烦,而且用例也略有不同。 My problems/needs were:我的问题/需求是:

  • I didn't quite understand the filter name as presented above.我不太明白上面介绍的过滤器名称。
  • The public function get_recipient() inside of class-wc-email.php expected a string but was getting an array. class-wc-email.php 中的public function get_recipient()需要一个字符串,但正在获取一个数组。
  • A also wanted to conditionally add the extra recipient based on the payment method, rather than postcode. A 还想根据付款方式有条件地添加额外的收件人,而不是邮政编码。

Here's what I did instead:这是我所做的:

  • Added full filter name, not just the suffix: woocommerce_email_recipient_new_order添加了完整的过滤器名称,而不仅仅是后缀: woocommerce_email_recipient_new_order
  • Replaced explode() and array_push() with string concatenation $email .= ',' . $additional_email;用字符串连接$email .= ',' . $additional_email;替换了explode()array_push() $email .= ',' . $additional_email; $email .= ',' . $additional_email; . .
  • Conditionally checked payment method: if( $order->get_payment_method() == "cod" ) .有条件检查的付款方式: if( $order->get_payment_method() == "cod" )

Full example:完整示例:

add_filter( 'woocommerce_email_recipient_new_order' , 'so_26429482_add_recipient', 20, 2 );
function so_26429482_add_recipient( $email, $order ) {

    // !empty($order) prevents a fatal error in WooCommerce Settings
    // !empty($email) prevents the duplicate email from being sent in cases where the filter is run outside of when emails are normally sent. In my case, when using https://wordpress.org/plugins/woo-preview-emails/
    if(!empty($order) && !empty($email)) {
    
        $additional_email = "isf@domain.co";
    
        if( $order->get_payment_method() == "cod" ) {
            $email .= ',' . $additional_email;
        } else {
            $email .= ',another@domain.co';
        }
    
    }
    return $email;
}

Each email has a filter that allows you to adjust the recipients of that email.每封电子邮件都有一个过滤器,可让您调整该电子邮件的收件人。 The filter name is essentially woocommerce_email_recipient_{$email_id} .过滤器名称本质上是woocommerce_email_recipient_{$email_id}

So the following would filter the "to" addresses for the "new_order" email.因此,以下内容将过滤“new_order”电子邮件的“to”地址。

add_filter( 'new_order' , 'so_26429482_add_recipient', 20, 2 );
function so_26429482_add_recipient( $email, $order ) {
    $additional_email = "somebody@somewhere.net";
    if( $order->shipping_postcode == "90210" ){
        $email = explode( ',', $email );
        array_push( $email, $additional_email );
    }
    return $email;
}

I'm not 100% certain on the conditional logic, but I think that should check the shipping zip code and subsequently send to the additional email address.我不是 100% 确定条件逻辑,但我认为应该检查运输邮政编码,然后发送到其他电子邮件地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在WooCommerce中应用特定优惠券代码时发送电子邮件通知 - Send an email notification when a specific coupon code is applied in WooCommerce 当在 WooCommerce 中应用特定优惠券代码时,向客户发送 email 通知 - Send an email notification to customer when a specific coupon code is applied in WooCommerce 需要帮助添加代码以将此发送到我的电子邮件地址 - Need Help Adding code to make this send to my email address 如何使用Drupal 7中的规则基于邮政编码接近度向用户发送电子邮件? - How to send users email based on zip code proximity using rules in Drupal 7? 将回复 email 地址更改为特定 WooCommerce email 通知 - Change reply-to email address to specific WooCommerce email notification 更改特定WooCommerce电子邮件通知的发件人姓名和电子邮件地址 - Change sender name and email address for specific WooCommerce email notifications 如何从特定的IP地址发送电子邮件? - How to send email from a specific ip address? 根据选择将联系表格发送到电子邮件地址 - Send contact form to email address based on selection 根据 Woocommerce 中的付款类型将新订单电子邮件发送到其他电子邮件 - Send New Order email to an additional email based on payment type in Woocommerce 当客户更改地址WooCommerce时发送通知电子邮件 - Send notification email when customer changes address WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM