YayMail - WooCommerce Email Customizer
  • Getting Started
    • Introduction
    • How to Install
    • Enter license key
    • System Requirements
    • How To Update YayMail
    • Uninstall and Delete
  • How It Works
    • Email Templates
    • Global Header and Footer
    • Settings
    • Addon
    • Preview Email
  • YayMail Main Customization Interface
    • The Elements Panel
      • Basic Elements
        • Embed a YayMail shortcode into a button
        • Add HTML and shortcode
        • Duplicate/Copy an Element
      • General Elements
      • WooCommerce Elements
        • Change Order Item Titles
      • Blocks
      • Drag and Drop Interface
      • Block-Based Live Editor
    • The Patterns Panel
      • Header
      • Footer
      • Banner
      • Pre-Designed Patterns
    • The Settings Panel
      • Global Settings
        • Customize with CSS
        • Hide Elements of Order Table
      • Email Settings
    • View History
  • Upper Toolbar
    • Shortcodes
    • Custom shortcode
    • Reset Templates
    • Import/copy template from
  • Visibility Control
  • Guides
    • Display coupons
    • Add column in order item table
  • Developer zone
    • Demo Plugin
    • Register Templates
      • Register new template
      • Email class
      • Render Templates content
    • Register Elements
      • Register New Elements
      • Element Class
      • Render Element content
      • Default element data
      • Custom Property Editor (coming soon)
    • Register Shortcodes
      • Register New Shortcodes
      • Shortcode Class
  • Integrations
    • Advanced Custom Fields (ACF) by WP Engine
    • Advanced Local Pickup for WooCommerce by Zorem
    • Back In Stock Notifier by codewoogeek
    • Checkout Field Editor by ThemeHigh
    • Checkout Field Editor by WooCommerce
    • Custom Order Status by Nuggethon
    • Custom Order Status by TycheSoftwares
    • Custom Order Status Manager for WooCommerce by Bright Plugins
    • Flexible Checkout Fields by WPDesk
    • FooEvents for WooCommerce
    • Shipment Tracking
    • Software Addon by WooCommerce
    • WooCommerce Admin Custom Order Fields by SkyVerge
    • WooCommerce Order Status Manager by SkyVerge
    • WooCommerce Show Attributes by Isabel Castillo
    • WPML & WooCommerce Multilingual
    • Weglot Translate
    • Loco Translate
    • TranslatePress/Polylang
    • Translate text in YayMail
    • User meta
  • Addons
    • Conditional Logic
    • WooCommerce Subscriptions
    • Germanized for WooCommerce
    • Dokan Multi Vendor
    • AutomateWoo
    • WooCommerce Follow Ups
    • WooCommerce Bookings
    • WooCommerce Gift Cards
    • Smart Coupons
    • WooCommerce Memberships and Teams for WooCommerce Memberships
    • WooCommerce Pre-Orders
    • Parcel Panel Order Tracking
    • WCFM Marketplace
    • Affiliate For WooCommerce
    • Back In Stock Notifications
    • PW WooCommerce Gift Cards
    • Appointments for WooCommerce
    • WooCommerce Bookings And Appointments by PluginHive
    • B2BKing
    • B2B Market
    • B2B & Wholesale Suite
    • TrackShip for WooCommerce
    • SG WooCommerce Order Approval
    • SUMO Subscriptions
    • SUMO Payment Plans
    • WooCommerce German Market
    • WC Vendors Marketplace
    • WooCommerce Print Invoices & Packing Lists
    • WooCommerce Deposits
    • WooCommerce Cart Abandonment Recovery by CartFlows
    • WooCommerce Waitlist
    • WooCommerce Simple Auctions
    • YITH Auctions for WooCommerce
    • YITH WooCommerce Stripe
    • YITH Membership
    • YITH Subscription
    • YITH Booking and Appointment
    • YITH WooCommerce Recover Abandoned Cart
    • YITH Wishlist
    • YITH Pre-Order
    • YITH Gift Cards
    • YITH Review For Discounts
    • YITH Multi Vendor
    • YITH WooCommerce Points and Rewards
    • YITH WooCommerce Request a Quote
    • YITH WooCommerce Affiliates
    • YITH WooCommerce Coupon Email System
    • YITH Advanced Refund System for WooCommerce
    • YITH Easy Login & Register Popup
    • YITH WooCommerce Delivery Date
    • WooCommerce Order Delivery Date Pro
    • Colissimo Shipping Methods for WooCommerce
    • WooCommerce Deposits & Partial Payments
    • Custom User Registration Fields by Addify
    • Quotes for WooCommerce
    • RMA Return Refund and Exchange For WooCommerce
    • License Manager for WooCommerce
    • WooCommerce Product Vendors
    • MultiVendorX
    • WooCommerce Stripe Payment Gateway
    • WooCommerce B2B
    • Order Cancellation Email To Customer
    • WooCommerce Order Delivery
    • Account Funds
    • TeraWallet for WooCommerce
    • Points And Rewards for WooCommerce
    • WooCommerce Split Orders
    • WP Crowdfunding
    • WooCommerce PDF Product Vouchers
    • MultiLocation Inventory & Order Routing
    • Woo Sell Services
    • WooCommerce Return & Warranty Management
    • WholesaleX
    • ShopMagic
    • WooCommerce Contact for Shipping Quote
    • MarketKing
    • AliDropship for WooCommerce
    • Custom Fields by Addify
  • Why Upgrade?
  • OTHER LINKS
    • FAQs
    • Troubleshooting
      • Continuous loading
      • Unable to save template
      • Unable to save Emoji
      • Unable to send test email
      • How to clear cache
      • How to use YayMail with GoDaddy
    • Changelog
    • Useful Links
      • AppSumo Deal
    • Support
      • How to pay without PayPal
      • How to renew the license key
      • How to change payment method
  • Articles
    • Design Tips
      • Customize Order Confirmation Email
      • Preview and Test Your Emails
      • Design a Black Friday Banner
      • Embed a Video in WooCommerce Email
      • Add Conditional Logic to WooCommerce Emails
    • Email Marketing
      • Setup Next Order Coupon in WooCommerce Emails
      • Automatically Send “Customer Invoice/Order Details" to Customers
      • Create Emails with Dynamic Content
      • Customize WooCommerce Membership Emails
      • Recover Abandoned Cart
    • Advanced Guides
      • Create WooCommerce Custom Emails Per Product
      • Customize ShopMagic Automation Emails
      • Create Custom Shortcode for YayMail
      • Translate Emails with Loco Translate
Powered by GitBook
On this page

Was this helpful?

  1. Developer zone
  2. Register Templates

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';
    }
}
PreviousRegister new templateNextRender Templates content

Last updated 21 days ago

Was this helpful?