When a Customer cancels a subscription, I send the cancellation email to both the Admin and the customer
/* 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 );
}
}
I need to send the Customer a different, custom email using an email template I created. I'm thinking I need to replace the code above with something like this
$wc_emails['WC_Email_Custom_Template']->$customer_email;
$wc_emails['WC_Email_Custom_Template']->trigger( $orderid );
but haven't found it yet.
UPDATE
This code will send two separate emails when a customer cancels a subscription; one to the email address that is the recipient (WooCommerce, Settings, Email, Cancelled Subscription, Recipient) and one to the customer. The custom customer email is saved to the theme's WooCommerce, Email folder and will override the WooCommerce default template file.
But the email doesn't get sent to the customer when the Admin cancels the subscription from the Edit Order screen.
/* 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();
$orderid = $subscription->get_order_number();
$wc_emails = WC()->mailer()->get_emails();
// Send email to both admin and customer
// $wc_emails['WC_Email_Cancelled_Order']->recipient .= ',' . $customer_email;
// Send email to admin only
$wc_emails['WC_Email_Cancelled_Order']->recipient;
$wc_emails['WC_Email_Cancelled_Order']->trigger( $orderid );
// Send email to customer only
$mailer = WC()->mailer();
$subject = __('Subscription Cancelled', 'custom');
$content = get_cancelled_subscription_content( $subscription, $subject, $mailer );
$headers = "Content-Type: text/html
";
$mailer->send( $customer_email, $subject, $content, $headers );
}
}
function get_cancelled_subscription_content( $subscription, $heading = false, $mailer ) {
$template = 'woocommerce/emails/cancelled-subscription.php';
return wc_get_template_html( $template, array(
'order' => $subscription,
'email_heading' => $heading,
'sent_to_admin' => true,
'plain_text' => false,
'email' => $mailer
) );
}