How to customize WooCommerce Emails

Customize WooCommerce Emails

Extend WC_Mail class and customize WooCommerce Emails

Read and follow all the given steps to customize WooCommerce emails. It will help you to add your email configuration in WC emails settings, use the header and footer template of WooCommerce emails and make it more professional.

Create a class and Extends WC_Mail class to customize WooCommerce Emails. (Step 1)

class {your class name) extends WC_Email

Override the necessary class members of WC_Email in the constructor of Class.(Step 2)

/**
 * Constructor.
 */
public function __construct() {
	$this->id             = '{unique_id}'; // Unique ID to Store Emails Settings
	$this->title          = __( 'Email Title', 'text_domain' ); // Title of email to show in Settings
	$this->customer_email = true; // Set true for customer email and false for admin email.
	$this->description    = __( 'Add description of your email.', 'text_domain ' ); // description of email
	$this->template_base  = YOUR_PLUGIN_DIR; // Base directory of template 
	$this->template_html  = 'templates/emails/{template_file_name}'; // HTML template path
	$this->template_plain = 'templates/emails/plain/{template_file_name}'; // Plain template path
	$this->placeholders = array( // Placeholders/Variables to be used in email
		'{member_name}'               => '',
		'{member_first_name}'         => '',
		'{member_last_name}'          => '',
		'{member_full_name}'          => ''
	);
	// Call to the  parent constructor.
	parent::__construct(); // Must call constructor of parent class
	// Trigger email action hook.
	add_action( 'membership_activated_email_notification', array( $this, 'trigger' ), 10, 2 ); // action hook(s) to trigger email. You can add multiple actions for one email. 
}

Change the default subject of the email by overriding the parent class method (Step 3)

public function get_default_subject() {
         return __( '[{site_title}]: I am default subject of email.', 'text_domain' );
}

Change the default heading of the email by overriding the parent class method (Step 4)

public function get_default_heading() {
	return __( 'I am default Heading', 'text_domain' );
}

Override the trigger method to replace your placeholders and send an email (Step 5)

public function trigger( $object_id, $object = false ) {
	$this->setup_locale();
	$this->object      = $object;
	// Write your Logic here
	// Assign values to placeholders
       $this->placeholders['{member_name}']  = $member_user->display_name;
       // It is necessary to assign the recipient email address
       $this->recipient  = $object->user_email;
	if ( $this->is_enabled() && $this->get_recipient() ) {
		$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
	}
	$this->restore_locale();
}

Override the get_content_html method to add your template of HTML email (Step 6)

public function get_content_html() {
	return wc_get_template_html(
		$this->template_html,
		array(
			'object'                     => $this->object,
			'email_heading'      => $this->get_heading(),
			'additional_content' => $this->get_additional_content(),
			'sent_to_admin'      => false,
			'plain_text'         => false,
			'email'              => $this,
		),
		‘’, // Add path to override the template in child theme or parent theme. 
		$this->template_base // Default path to template file in your plugin
	);
}

Note: Path and default path in wc_get_template_html() can be defined as:

The path is a defined path to override the email template and the default path is a path to your plugin template.

Read more about wc_get_template and wc_locate_template() to understand the over-riding templates in WooCommerce.

Override the get_content_plain method to add your template of plain email (Step 7)

public function get_content_plain() {
	return wc_get_template_html(
		$this->template_html,
		array(
			'object'              => $this->object,
			'email_heading'      => $this->get_heading(),
			'additional_content' => $this->get_additional_content(),
			'sent_to_admin'      => false,
			'plain_text'         => false,
			'email'              => $this,
		),
		‘’,
		$this->template_base
	);
}

Note: Necessary modification for a class is completed.

Customize woocommerce emails template
Email Template Design

Add a hook in your main file to add email settings in WooCommerce Settings and create an instance of your class. (Step 8)

add_filter( 'woocommerce_email_classes', array( $this, 'your function' ), 90, 1 );

function your-function ( $emails ){
	// include your class file.
	include_once _PLUGIN_DIR . '/includes/emails/admin/class -membership-activated.php';
	//create an instance of file and set in a unique index of emails array. 
	$emails['membership_activated'] = new  Membership_Activated();
	//return emails array.
	return $emails;
} 

Call the mailer function of WooCommerce, Trigger the action hook and pass the necessary variables. (Step 9)

wc()->mailer();
do_action('membership_activated_email_notification', 'your object or id', 'object/id');

Alternatively, you can call the trigger function directly to send the email.

wc()->mailer->emails[‘membership_activated']->trigger(‘object id’, ‘object’);

Note: customize WooCommerce Emails

Note: It is necessary to store email settings once and activate your email.

Read More about customizing WooCommerce Emails


Leave a Reply

Scroll to Top