WooCommerce订阅取消电子邮件发送给客户

时间:2019-08-01 22:25:50

标签: php wordpress woocommerce

我正在使用WooCommerce订阅。我已经自定义了取消订阅模板,当客户取消订阅时,它可以工作,将自定义电子邮件发送给管理员,但是我无法获得将取消电子邮件发送给客户的信息。

我尝试修改在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 );
    } 
}

但管理员或客户均未收到电子邮件。

编辑1

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

/* 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 );
    } 
}

1 个答案:

答案 0 :(得分:0)

感谢您分享您的食谱。我的目标是相同的-向客户发送一封电子邮件,其中包含订阅取消确认。

我对您的解决方案进行了一些更新,最后得到以下内容。

  1. 我们可以在某些订阅状态更改上安装钩子,因此可以避免在函数内部进行状态检查
/* Send email to a customer on cancelled subscription in WooCommerce */
add_action( 'woocommerce_subscription_status_pending-cancel', 'sendCustomerCancellationEmail' );
  1. 我们可以使用WCS_Email_Cancelled_Subscription类来触发相应的电子邮件。在这种情况下,触发函数需要一个订阅对象。
/**
 * @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 );
}