Email class
Creating a Custom Email Class
To build a custom email class in WooCommerce with YayMail, you’ll need to extend the BaseEmail class provided by YayMail. Here’s a clear example to guide you in setting up your custom email class:
Extending BaseEmail:
class YourNewEmail extends \YayMail\Abstracts\BaseEmail { private static $instance; /** * Defines the email type * 1. YAYMAIL_WITH_ORDER_EMAILS: emails that include order information * 2. YAYMAIL_NON_ORDER_EMAILS: emails that do not include order information * Default is YAYMAIL_WITH_ORDER_EMAILS if not set */ public $email_types = [ YAYMAIL_WITH_ORDER_EMAILS ]; public static function instance() { if ( null === static::$instance ) { static::$instance = new static(); } return static::$instance; } protected function __construct() { // This is your custom id. // It should be the same id as the WooCommerce email for easily handling. $this->id = 'your_new_email_template_id'; $this->title = 'Your New Email'; $this->recipient = 'Customer'; // Customer/Admin or Other sources. $this->source = [ 'plugin_id' => '3rd-plugin-slug', // This id is what ever you want 'plugin_name' => 'The plugin email belong to', ]; } }Start by defining a new class that extends the BaseEmail class provided by YayMail. Make sure to include the following essential variables and methods for the class:
Variables:
id: A unique identifier for the email.title: The title of the email, displayed in the YayMail Editor or settings.recipient: Specifies who receives the email (e.g., Customer or Admin).source: An array with plugin details, such as plugin_id and plugin_name.email_types: Defines the email type.
By following these steps, you can create a custom email template that works smoothly with your WooCommerce and YayMail setup.
- Function:
get_default_delementsThe
get_default_elementsfunction is designed to retrieve and return a set of default elements using theElementsLoader::load_elementsmethod. Below is a breakdown of its components:Logo Element: A basic component identified by its type 'Logo'.
Heading Element: This element includes a 'Heading' type with specified attributes, notable for its 'rich_text' attribute containing the text 'Email Heading'.
Text Element: Main content for this email
Footer Element: A basic email footer for this email
To integrate a hook for triggering an email within the constructor, use the default method provided by the
BaseEmailclass. Alternatively, you can override this function if customization is required.Add a hook to send mail:
To return the path of the 'new-email-1.php' template, the function
get_template_filegenerates the content as follows:
We have followed the steps provided, and the complete file is now
Last updated
Was this helpful?