WooCommerce:添加其他电子邮件收件人(新订单),具体取决于邮政编码

时间:2018-10-02 17:32:24

标签: php wordpress email woocommerce postal-code

我想根据客户的邮政编码添加其他电子邮件收件人。我们有不同省份的供应商(例如4614-供应商1,3314-供应商2),有必要将订单定向到负责的供应商。对于会计,我们需要将新订单定向到负责部门(当前在WooCommerce后端中设置)。

我已经尝试对电子邮件收件人使用WooCommerce筛选器,以根据邮政编码添加电子邮件。为此,我设置了两个数组,并尝试获取要在此if函数中使用的装运邮政编码(或帐单邮政编码)。

这是我尝试在我们的website上使用的代码:

add_filter( 'woocommerce_email_recipient_new_order', 'cond_recipients_email_notifications', 10, 2 );
function cond_recipients_email_notifications( $recipient, $order ) {

if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;

// TARGET-ZIP-CODE
$zip_zone1 = array( '4614', '4072', '4615', '4064', '4062', '4611' );
$zip_zone2 = array( '3314', '3353', '3313', '3312', '3350', '3322', '3354' );

// User ZIP-CODE 
$user_zip_zone =  $order->get_shipping_postcode();
if(empty($user_shipping_postcode))
    $user_zip_zone = $order->get_billing_postcode();

// ADD EMAIL IF ZIP-CODE MATCHES TARGET-ZIP-CODE
if ( in_array( $user_zip_zone, $zip_zone1)) {
            $recipient .= ', e-mail-supplier1@e-mail.at';
    } elseif ( in_array( $user_zip_zone, $zip_zone2) ) {
    $recipient .= ', e-mail-supplier2@e-mail.at';
    }

return $recipient;
}

我试图在我的子主题的functions.php中实现此代码段,但是它没有按预期工作。我显然错过了一些东西,但是我不知道我的错误可能是什么。不幸的是,我对php的了解很少,因此我对此事的理解受到限制。

有人知道在WooCommerce中实现所需功能的方法还是如何使此代码段起作用?我的代码有什么问题?

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我得到此代码正常运行的信息。收到此信息后,我检查了我的服务器设置,发现某些错误的设置。

代码按预期运行!谢谢您的帮助!

随意使用我的代码,也许其他人需要实现相同的功能。