Search code examples
phpwordpresstemplateswoocommerceplugins

Send custom email using WooCommerce template from a custom plugin


I am trying to send email from my plugin using standard WooCommerce layout but I would like to use my own template located in my plugin. When I move my template to /wp-content/plugins/woocommerce/templates/emails this code works:

function get_custom_email_html( $heading = false, $mailer ) {

    $template = 'emails/my-custom-email-i-want-to-send.php';

    return wc_get_template_html( $template, array(      
        'email_heading' => $heading,
        'sent_to_admin' => false,
        'plain_text'    => false,
        'email'         => $mailer
    ) );

}

// load the mailer class
$mailer = WC()->mailer();

//format the email
$recipient = "someone@somewhere.com";
$subject = __("Hi! Here is a custom notification from us!", 'theme_name');
$content = get_custom_email_html( $subject, $mailer );
$headers = "Content-Type: text/html\r\n";

//send the email through wordpress
$mailer->send( $recipient, $subject, $content, $headers );

I assume that I need to add a path to get_custom_email_html() but I am not sure how. Can somebody give me a hint, please? My template is a very simple:

/*
 * @hooked WC_Emails::email_header() Output the email header
 */
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
<p><?php esc_html_e( 'This a test', 'my-plugin' ); ?></p>
<?php
/*
 * @hooked WC_Emails::email_footer() Output the email footer
 */
do_action( 'woocommerce_email_footer', $email );

Solution

  • There are some missing things in your plugin code, like the plugin path that requires to be defined when using wc_get_template_html().

    For testing, I have trigger the email using a function hooked in template_redirect action hook when browsing in front end, restricted to "administrator" user role.

    I have changed the template name and path: In your plugin folder, the file is now located in: templates/emails/my-custom-email.php.

    The code:

    function my_plugin_path(){
        return untrailingslashit( plugin_dir_path( __FILE__ ) ); 
    }
    
    function get_custom_email_html( $mailer, $email_heading = false ) {
        $template = 'emails/my-custom-email.php';
    
        return wc_get_template_html( $template, array(      
            'email_heading' => $email_heading,
            'sent_to_admin' => false,
            'plain_text'    => false,
            'email'         => $mailer
        ), '', my_plugin_path() . "/templates/" );
    }
    
    add_action('template_redirect', 'send_custom_email_notifications');
    function send_custom_email_notifications(){
        if ( current_user_can('administrator')) {
            // Get the WC_Emails object
            $mailer = WC()->mailer();
    
            // Define variable arguments for the email
            $recipient = "someone@somewhere.com";
            $subject = __("Hi! Here is a custom notification from us!", 'theme_name');
            $content = get_custom_email_html( $mailer, $subject );
            $headers = "Content-Type: text/html\r\n";
    
            // Send the email using WooCommerce WC_Emails send() method
            $mailer->send( $recipient, $subject, $content, $headers );
        }
    }
    

    Tested, it works.

    Note: I have used your Email template HTML content.