【问题标题】:Sending email to customer on cancelled order in Woocommerce在 Woocommerce 中向客户发送取消订单的电子邮件
【发布时间】:2017-12-05 07:37:08
【问题描述】:

我正在尝试在订单被取消时向客户发送电子邮件。默认情况下,woocommerce 仅将此电子邮件发送给站点管理员。 此代码解决了网络上相关帖子的问题:

function wc_cancelled_order_add_customer_email( $recipient, $order ){
   return $recipient . ',' . $order->billing_email;
}
add_filter( 'woocommerce_email_recipient_cancelled_order', 'wc_cancelled_order_add_customer_email', 10, 2 );
add_filter( 'woocommerce_email_recipient_failed_order', 'wc_cancelled_order_add_customer_email', 10, 2 );

但是,woocommerce 似乎完全删除了这些过滤器挂钩。 有没有办法做到这一点?

提前致谢!

【问题讨论】:

    标签: php wordpress woocommerce orders email-notifications


    【解决方案1】:

    在这个挂在 woocommerce_order_status_changed 动作钩子中的自定义函数中,我的目标是“取消”和“失败”的订单向客户发送相应的电子邮件通知(因为管理员会在他的WooCommerce 自动通知旁边):

    add_action('woocommerce_order_status_changed', 'send_custom_email_notifications', 10, 4 );
    function send_custom_email_notifications( $order_id, $old_status, $new_status, $order ){
        if ( $new_status == 'cancelled' || $new_status == 'failed' ){
            $wc_emails = WC()->mailer()->get_emails(); // Get all WC_emails objects instances
            $customer_email = $order->get_billing_email(); // The customer email
        }
    
        if ( $new_status == 'cancelled' ) {
            // change the recipient of this instance
            $wc_emails['WC_Email_Cancelled_Order']->recipient = $customer_email;
            // Sending the email from this instance
            $wc_emails['WC_Email_Cancelled_Order']->trigger( $order_id );
        } 
        elseif ( $new_status == 'failed' ) {
            // change the recipient of this instance
            $wc_emails['WC_Email_Failed_Order']->recipient = $customer_email;
            // Sending the email from this instance
            $wc_emails['WC_Email_Failed_Order']->trigger( $order_id );
        } 
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    这应该适用于 WooCommerce 3+

    如果需要,您可以将其添加到现有收件人,而不是更改电子邮件:

    // Add a recipient in this instance
    $wc_emails['WC_Email_Failed_Order']->recipient .= ',' . $customer_email;
    

    相关回答:Send an email notification when order status change from pending to cancelled

    【讨论】:

    • 成功了,谢谢!你真的拯救了我的一天,我在这个问题上坐得太久了。祝你有个美好的一天!
    • 不错的解决方案,有没有办法更改电子邮件的内容文本?我希望管理员通知文本与客户不同。 gettext 会是答案吗?
    • 祝福这个动作,对于那些想要自定义电子邮件的人,我认为他们应该在这里看到:skyverge.com/blog/how-to-add-a-custom-woocommerce-email。只需禁用该插件中的触发器即可。你可以使用这个函数来触发 $wc_emails['WC_Expedited_Order_Email']->trigger( $order_id );
    • 您也可以在woocommerce电子邮件中自定义电子邮件模板。
    猜你喜欢
    • 2017-08-08
    • 2019-01-09
    • 2014-03-09
    • 2020-06-03
    • 2017-08-19
    • 2016-02-23
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多