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



基本上,在woocommerce->设置->电子邮件->新订单中,您可以选择输入多个电子邮件地址(用逗号分隔)来发送完成的订单。但我需要一种方法,根据订购产品的客户的邮政编码,只发送给其中一个收件人。或者完全改写wooccommerce的处理方式。

我如何才能联系到负责此事的职能部门,以便发送给正确的收件人?基本上,是否为此定义了挂钩,或者是否存在类似的插件,或者我是否必须编辑WooCommerce的核心文件?如果需要编辑核心文件,有人能告诉我哪些文件需要编辑吗?

我对helgatheviking上面的回答有点麻烦,还有一个稍微不同的用例。我的问题/需求是:

  • 我不太理解上面介绍的过滤器名称
  • class-wc-email.php内部的public function get_recipient()需要一个字符串,但得到的却是一个数组
  • A还希望根据付款方式而不是邮政编码有条件地添加额外的收件人

以下是我所做的:

  • 添加了完整的过滤器名称,而不仅仅是后缀:woocommerce_email_recipient_new_order
  • 用串接$email .= ',' . $additional_email;替换explode()array_push()
  • 有条件检查的付款方式:if( $order->get_payment_method() == "cod" )

完整示例:

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;
}

每封电子邮件都有一个过滤器,允许您调整该电子邮件的收件人。过滤器名称实质上是woocommerce_email_recipient_{$email_id}

因此,下面将筛选"new_order"电子邮件的"收件人"地址。

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;
}

我不能100%确定条件逻辑,但我认为应该检查发货邮政编码,然后发送到其他电子邮件地址。

最新更新