skip to Main Content

Woocommerce subscriptions by default doesn’t send subscription cancellation to the customer so I’m trying to add that by using woocommerce_subscription_status_pending-cancelHook and the function is below:

add_action('woocommerce_subscription_status_pending-cancel', 'yanco_woocommerce_subscription_status_pending_cancel', 10, 3 );
function yanco_woocommerce_subscription_status_pending_cancel( $subscription ) {
    $customer_email = $subscription->get_billing_email();
    $wc_emails = WC()->mailer()->get_emails();

    $admin_email = $wc_emails['WCS_Email_Cancelled_Subscription']->recipient;
    $wc_emails['WCS_Email_Cancelled_Subscription']->trigger( $subscription );

    $wc_emails['WCS_Email_Cancelled_Subscription']->recipient = $customer_email;
    $wc_emails['WCS_Email_Cancelled_Subscription']->trigger( $subscription );
}

But this code isn’t working when I click on the Cancel button in my account it keep circling without any response after a refresh it cancels the subscription but not sending email to customer.

I tried adding the code above and was expecting the emails to be sent out to the customer when they cancel their subscription.

2

Answers


  1. Chosen as BEST ANSWER

    This has fixed the issue:

    add_filter('woocommerce_email_recipient_cancelled_subscription', 'set_customer_as_cancelled_subscription_recipient', 10, 2);
    

    function set_customer_as_cancelled_subscription_recipient($recipient, $subscription) { return $subscription ? $subscription->get_billing_email() : $recipient; }`


  2. Your suggested add_filter('woocommerce_email_recipient_cancelled_subscription'
    filter is what I found works as well.

    One possible modification: If you want to send the cancel email to the customer PLUS still send it to whoever you have set as the recipient in WooCommerce Email Settings (such as an internal admin user for example), this works:

    add_filter('woocommerce_email_recipient_cancelled_subscription', 'set_customer_as_cancelled_subscription_recipient', 10, 2);
    function set_customer_as_cancelled_subscription_recipient($recipient, $subscription) {
    
        // Append the customer's billing email if it's not already in the recipient list in addition to the recipient specified in WooCommerce Email settings
        if ($subscription && false === strpos($recipient, $subscription->get_billing_email())) {
            $recipient .= ',' . $subscription->get_billing_email();
        }
        return $recipient;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search