Woocommerce Subscription 'WC_Email' not found error

76 Views Asked by At

I am trying to add a custom email when user subscription status is changed from Active to pending-cancel in Woocommerce subscription.

I have added the following code in my theme functions.php but it triggers fatal error and says "PHP Fatal error: Uncaught Error: Class 'WC_Email' not found in /wp-content/themes/twentytwentyone/functions.php:679"

Here is the complete code

add_action('woocommerce_email_classes', 'add_custom_pending_cancel_email_class');
add_action('woocommerce_subscription_status_pending-cancel', 'custom_send_pending_cancel_email');

function add_custom_pending_cancel_email_class($email_classes) {
    $email_classes['WC_Email_Custom_Pending_Cancel'] = new WC_Email_Custom_Pending_Cancel();
    return $email_classes;
}

function custom_send_pending_cancel_email($subscription) {
    if ($subscription->get_status() === 'pending-cancel') {
        $email_heading = 'Your Email Heading';
        $email = WC()->mailer()->emails['WC_Email_Custom_Pending_Cancel'];
        $email->trigger($subscription, $email_heading);
    }
}

 class WC_Email_Custom_Pending_Cancel extends WC_Email {

    public function __construct() {
        $this->id = 'custom_pending_cancel';
        $this->customer_email = true;
        $this->title = 'Custom Pending Cancel';
        $this->template_html = 'woocommerce/emails/custom-pending-cancel-email.php';
        $this->template_plain = 'woocommerce/emails/plain/custom-pending-cancel-email.php';
        $this->placeholders = array(
            '{site_title}' => $this->get_blogname(),
        );
        parent::__construct();
    }

    public function trigger($subscription, $email_heading = '') {
        $this->setup_locale();

        $this->object = $subscription;
        $this->recipient = $subscription->get_billing_email();
        $this->email_heading = $email_heading;

        $this->send($this->recipient, $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments());

        $this->restore_locale();
    }

}
0

There are 0 best solutions below