WooCommerce更改电子邮件收件人取决于航运国家



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

我们正在使用PayPal高级处理付款从我们的网站内通过iframe。

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

$woocommerce->customer->shipping_country

$woocommerce->session->customer['shipping_country'];

本地我没有paypal高级激活,所以当测试那里它将工作。然而,在生产服务器上我们使用它,这就是问题发生的地方。当过滤器试图抓取客户的发货订单时,这些全局对象为空。这让我相信,一旦PayPal订单完成,当前页面被重定向到包含适当信息的感谢页面,然而,当过滤器运行时,全局变量是空的。

话虽如此,当woocommerce_email_recipient_new_order运行时,我如何获取客户的送货地址信息?

一旦下了订单,您需要从$order对象而不是从会话中检索信息(例如运输国家)。订单在这里作为第二个参数传递给woocommerce_email_recipient_new_order过滤器。

下面是一个示例,说明如何将order对象传递给过滤器的回调函数,并使用它来修改收件人:

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和以前的版本兼容。

最新更新