WooCommerce 订阅取消电子邮件给客户 [英] WooCommerce Subscription Cancellation Email to Customer

查看:18
本文介绍了WooCommerce 订阅取消电子邮件给客户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WooCommerce 订阅.我已经自定义了取消订阅模板,它在客户取消订阅时起作用,将自定义电子邮件发送给管理员,但我无法将取消电子邮件发送给客户.

I am using WooCommerce Subscriptions. I've customized the cancelled-subscription template and it works when a customer cancels a subscription, sending the custom email to the admin but I'm unable to get it to send the cancellation email to the customer.

我尝试修改在 stackoverflow 上找到的代码

I've tried adapting code found on stackoverflow

/* Send email to customer on cancelled order in WooCommerce */
add_action('woocommerce_subscription_status_updated', 'send_custom_email_notifications', 10, 3 );
function send_custom_email_notifications( $subscription, $new_status, $old_status ){
    if ( $new_status == 'cancelled' || $new_status == 'pending-cancel' ){
        $wc_emails = WC()->mailer()->get_emails(); // Get all WC_emails objects instances

        $customer_email = $subscription->get_billing_email(); // The customer email

        $wc_emails['WC_Email_Cancelled_Order']->recipient .= ',' . $customer_email;
        $wc_emails['WC_Email_Cancelled_Order']->trigger( $subscription->id );
    } 
}

但管理员或客户都没有收到电子邮件.

but neither admin or customer gets the email.

编辑 1

我终于可以使用此更新后的代码向管理员和客户发送订阅取消电子邮件

I'm finally able to send the Subscription Cancellation Email to both Admin and the Customer with this updated code

/* Send email to customer on cancelled order in WooCommerce */
add_action('woocommerce_subscription_status_updated', 'send_custom_email_notifications', 10, 3 );
function send_custom_email_notifications( $subscription, $new_status, $old_status ){
    if ( $new_status == 'cancelled' || $new_status == 'pending-cancel' ){
        $customer_email = $subscription->get_billing_email();
        $userid = $subscription->get_user_id();
        $wc_emails = WC()->mailer()->get_emails();
        $wc_emails['WC_Email_Cancelled_Order']->recipient .= ',' . $customer_email;
        $wc_emails['WC_Email_Cancelled_Order']->trigger( $orderid );
    } 
}

推荐答案

感谢您分享您的食谱.我有同样的目标 - 向客户发送一封电子邮件,确认订阅取消.

Thank you for sharing your recipie. I've had the same goal - to send an email to customer with a subscription cancellation confirmation.

我对您的解决方案进行了一些更新,结果如下.

I did some updates to your solution and ended up with the following.

  1. 我们可以在某些订阅状态更改时安装钩子,这样我们就可以避免在函数内部进行状态检查

/* Send email to a customer on cancelled subscription in WooCommerce */
add_action( 'woocommerce_subscription_status_pending-cancel', 'sendCustomerCancellationEmail' );

  1. 我们可以使用WCS_Email_Cancelled_Subscription 类来触发相应的电子邮件.在这种情况下,触发器函数需要订阅对象.
  1. We can use WCS_Email_Cancelled_Subscription class to trigger corresponding email. Trigger function requires a subscription object in that case.

/**
 * @param WC_Subscription $subscription
 */
function sendCustomerCancellationEmail( $subscription ) {
    $customer_email = $subscription->get_billing_email();
    $wc_emails = WC()->mailer()->get_emails();
    $wc_emails['WCS_Email_Cancelled_Subscription']->recipient = $customer_email;
    $wc_emails['WCS_Email_Cancelled_Subscription']->trigger( $subscription );
}

这篇关于WooCommerce 订阅取消电子邮件给客户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆