WooCommerce根据运送国家/地区更改电子邮件收件人 [英] WooCommerce Change Email Recipient Depending on Shipping Country

查看:191
本文介绍了WooCommerce根据运送国家/地区更改电子邮件收件人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据客户的收货地址将某些电子邮件动态添加到新订单收件人列表中。

I am trying to dynamically add certain emails to the new order recipients list based on the customers ship-to address.

我们正在使用PayPal Advanced来通过n iframe从我们网站内处理付款。

we are using PayPal advanced to process payments from within our site via n iframe.

问题是切换电子邮件的过滤器使用的是我从两个地方之一获取的客户的收货地址:

The problem is that the filter that switches the email uses the customer's ship-to address which I am getting from one of two places:

$ woocommerce-> customer-> shipping_country

$ woocommerce->会话-> customer ['shipping_country'];

在本地我没有激活高级贝宝,所以当在那里测试时,它将起作用。但是,在生产服务器上,我们正在使用它,这就是问题所在。当过滤器尝试获取客户的装运订单时,这些全局对象为空。这使我相信,完成PayPal订单后,当前页面将重定向到其中包含适当信息的谢谢页面,但是运行过滤器时全局变量为空。

Locally I don't have paypal advanced activated, so when test there it will work. However on the production server we are using it, and that's where the issue occurs. Those global objects are empty when the filter attempts to grab the customer's shipment order. This leads me to believe that once the PayPal order is done, the current page is redirected to the thank-you page with the proper information contained within it, however the global variables are empty when the filters are run.

这样说, woocommerce_email_recipient_new_order 运行时,如何获取客户的收货地址信息?

With that said, how do I fetch the customer's shipping address info when woocommerce_email_recipient_new_order runs?

推荐答案

下订单后,您需要从 $中检索信息(例如运送国家) order 对象,而不是来自会话。订单被传递到 woocommerce_email_recipient_new_order 过滤器此处作为第二个参数。

Once the order is placed you need to retrieve information (such as shipping country) from the $order object and not from the session. The order is passed to the woocommerce_email_recipient_new_order filter here as the second argument.

以下是如何将订单对象传递到的示例您的过滤器的回调并使用它来修改收件人:

Here's an example of how you would pass the order object to your filter's callback and use it to modify the recipient:

function so_39779506_filter_recipient( $recipient, $order ){

    // get the shipping country. $order->get_shipping_country() will be introduced in WC2.7. $order->shipping_country is backcompatible
    $shipping_country = method_exists( $order, 'get_shipping_country') ) ? $order->get_shipping_country() : $order->shipping_country;

    if( $shipping_country == 'US' ){

        // Use this to completely replace the recipient.
        $recipient = 'stack@example.com';

        // Use this instead IF you wish to ADD this email to the default recipient.
        //$recipient .= ', stack@example.com';
    }
    return $recipient;
}
add_filter( 'woocommerce_email_recipient_new_order', 'so_39779506_filter_recipient', 10, 2 );

经过编辑以使代码与WooCommerce 2.7和早期版本兼容。

Edited to make code compatible with both WooCommerce 2.7 and previous versions.

这篇关于WooCommerce根据运送国家/地区更改电子邮件收件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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