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

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

问题是切换电子邮件的过滤器使用客户的收货地址,我是从以下两个地方之一获得的:
$woocommerce->customer->shipping_country$woocommerce->session->customer['shipping_country'];
在本地我没有激活 paypal advanced,所以在那里测试时它会工作。但是在我们使用的生产服务器上,这就是问题发生的地方。当过滤器尝试获取客户的发货订单时,这些全局对象为空。这让我相信,一旦 PayPal 订单完成,当前页面将重定向到包含正确信息的感谢页面,但是在运行过滤器时全局变量为空。

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

最佳答案

下订单后,您需要从 $order 对象而不是 session 中检索信息(例如运送国家/地区)。该订单作为第二个参数传递给 woocommerce_email_recipient_new_order 过滤器 here

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

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 = '[email protected]';

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

编辑以使代码与 WooCommerce 2.7 和以前的版本兼容。

关于php - WooCommerce 根据运送国家/地区更改电子邮件收件人,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39779506/

10-11 12:07