WooCommerce Email Generator
A WC_Email subclass plus its own HTML template — with template_base set so wc_get_template_html() actually finds the plugin's file instead of falling through to core's.
The email
Acme_Loyalty_Earned_Email
Recipient
Trigger
Template intro line
<?php // Generated with WP CodeKit — powered by GrowQuest (https://growquest.io). /** * Plugin Name: Loyalty points earned email * Description: Registers the acme_loyalty_earned transactional email. * Version: 1.0.0 * Requires PHP: 7.4 * Requires Plugins: woocommerce * Text Domain: acme */ defined( 'ABSPATH' ) || exit; /** * Declare the class once WooCommerce's own email base class exists. */ function acme_email_init() { if ( class_exists( 'Acme_Loyalty_Earned_Email' ) ) { return; } class Acme_Loyalty_Earned_Email extends WC_Email { public function __construct() { $this->id = 'acme_loyalty_earned'; $this->customer_email = true; $this->title = __( 'Loyalty points earned', 'acme' ); $this->description = __( 'Sent to the customer once their order is marked complete, showing the points they earned.', 'acme' ); $this->heading = __( 'You earned loyalty points!', 'acme' ); $this->subject = __( 'You earned points on order #{order_number}', 'acme' ); $this->template_html = 'emails/acme-loyalty-earned.php'; $this->template_plain = 'emails/plain/acme-loyalty-earned.php'; $this->template_base = plugin_dir_path( __FILE__ ) . 'templates/'; add_action( 'woocommerce_order_status_completed', array( $this, 'trigger' ), 10, 2 ); parent::__construct(); } /** * The settings fields, keyed by option name. */ public function init_form_fields() { $this->form_fields = array( 'enabled' => array( 'title' => __( 'Enable/Disable', 'acme' ), 'type' => 'checkbox', 'label' => __( 'Enable this email notification', 'acme' ), 'default' => 'yes', ), 'subject' => array( 'title' => __( 'Subject', 'acme' ), 'type' => 'text', 'default' => '', 'placeholder' => __( 'You earned points on order #{order_number}', 'acme' ), 'desc_tip' => true, ), 'heading' => array( 'title' => __( 'Email heading', 'acme' ), 'type' => 'text', 'default' => '', 'placeholder' => __( 'You earned loyalty points!', 'acme' ), 'desc_tip' => true, ), ); } /** * Fires on woocommerce_order_status_completed. Sends the email if it is enabled and there is a recipient. * * @param int $order_id Order ID. * @param WC_Order $order Order object, when core already has it loaded. */ public function trigger( $order_id, $order = false ) { $this->setup_locale(); if ( $order_id && ! is_a( $order, 'WC_Order' ) ) { $order = wc_get_order( $order_id ); } if ( is_a( $order, 'WC_Order' ) ) { $this->object = $order; $this->recipient = $this->object->get_billing_email(); } if ( ! $this->is_enabled() || ! $this->get_recipient() ) { $this->restore_locale(); return; } $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); $this->restore_locale(); } /** * The HTML version, rendered through the plugin's own template file. */ public function get_content_html() { return wc_get_template_html( $this->template_html, array( 'order' => $this->object, 'email_heading' => $this->get_heading(), 'sent_to_admin' => false, 'plain_text' => false, 'email' => $this, ), '', $this->template_base ); } /** * The plain-text version. */ public function get_content_plain() { return wc_get_template_html( $this->template_plain, array( 'order' => $this->object, 'email_heading' => $this->get_heading(), 'sent_to_admin' => false, 'plain_text' => true, 'email' => $this, ), '', $this->template_base ); } } } add_action( 'plugins_loaded', 'acme_email_init' ); /** * Register it so it appears under WooCommerce → Settings → Emails. * * @param array $emails Existing email classes, keyed by class name. * @return array */ function acme_add_email_class( $emails ) { $emails['Acme_Loyalty_Earned_Email'] = new Acme_Loyalty_Earned_Email(); return $emails; } add_filter( 'woocommerce_email_classes', 'acme_add_email_class' );