gpt4 book ai didi

php - WooCommerce 订阅取消电子邮件给客户

转载 作者:行者123 更新时间:2023-12-04 17:34:11 28 4
gpt4 key购买 nike

我正在使用 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. 我们可以在某些订阅状态更改时安装钩子(Hook),这样我们就可以避免在函数内部进行状态检查
/* 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 );
}

关于php - WooCommerce 订阅取消电子邮件给客户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57317710/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com