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:

  1. 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.

  2. Function: get_default_delements

    The get_default_elements function is designed to retrieve and return a set of default elements using the ElementsLoader::load_elements method. 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

public function get_default_elements() {
        $default_elements = \YayMail\Elements\ElementsLoader::load_elements(
            [
                [
                    'type' => 'Logo',
                ],
                [
                    'type'       => 'Heading',
                    'attributes' => [
                        'rich_text' => 'Email Heading',
                    ],
                ],
                [
                    'type'       => 'Text',
                    'attributes' => [
                        'rich_text' => '<p><span>Email content</span></p>',
                    ],
                ],
                [
                    'type' => 'Footer',
                ],
            ]
        );

        return $default_elements;
    }

  1. To integrate a hook for triggering an email within the constructor, use the default method provided by the BaseEmail class. Alternatively, you can override this function if customization is required.

    Add a hook to send mail:

add_filter( 'wc_get_template', [ $this, 'get_template_file' ], 10, 1 );

  1. To return the path of the 'new-email-1.php' template, the function get_template_file generates the content as follows:

    return __DIR__ . '/templates/your-new-email-template.php';

We have followed the steps provided, and the complete file is now

/**
 * NewEmail1 Class
 *
 * @method static YourNewEmailTemplate get_instance()
 */
class YourNewEmail extends \YayMail\Abstracts\BaseEmail {
    private static $instance;
    
    public static function instance() {
	if ( null === static::$instance ) {
		static::$instance = new static();
	}
	return static::$instance;
    }
    
    public $email_types = [ YAYMAIL_WITH_ORDER_EMAILS ];

    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',
        ];
    }
    
    // This function will get default elements of new template
    public function get_default_elements() {
        $default_elements = \YayMail\Elements\ElementsLoader::load_elements(
            [
                [
                    'type' => 'Logo',
                ],
                [
                    'type'       => 'Heading',
                    'attributes' => [
                        'rich_text' => 'Email Heading',
                    ],
                ],
                [
                    'type'       => 'Text',
                    'attributes' => [
                        'rich_text' => '<p><span>Email content</span></p>',
                    ],
                ],
                [
                    'type' => 'Footer',
                ],
            ]
        );

        return $default_elements;
    }


    public function get_template_path() {
        return __DIR__ . '/templates/your-new-email-template.php';
    }
}

Last updated

Was this helpful?