Support » Plugin: WooCommerce » Preview follow-up mail templates

  • Resolved Aklesh

    (@aklesh)


    Hi,

    How can I preview the emails that I am sending out to customers and admin (i.e. admin-new-order etc.). I used to do this by altering the woocommerce_preview_emails() function in woocommerce-admin-functions.php. This function is removed in Woocommerce 2.x.

    Thanks!
    Aklesh

    https://wordpress.org/plugins/woocommerce/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi again,

    I solved this by doing the following:

    Add to functions.php:

    function preview_email()
    {
        $orderId  = 914;
        $email_class = 'WC_Email_New_Order';
    
        $wc_emails = new WC_Emails();
        $emails = $wc_emails->get_emails();
    
        $new_email = $emails[$email_class];
        $new_email->trigger($orderId);
    
        echo $new_email->get_content();
    
        return null;
    }
    
    add_action('wp_ajax_previewemail', 'preview_email');

    call in browser:
    http://yousite.com/wp-admin/admin-ajax.php?action=previewemail

    There are important variables in the preview_email function:
    $orderId => put here an orderId from you order database
    $email_class => put here the email template you want to preview.

    choose one of the template files:
    WC_Email_Customer_Note
    WC_Email_Customer_Completed_Order
    WC_Email_Customer_New_Account
    WC_Email_Customer_Reset_Password
    WC_Email_Customer_Processing_Order
    WC_Email_Customer_Invoice
    WC_Email_New_Order

    If you don’t want to make changes in the function, you can also so something like this:

    http://yousite.com/wp-admin/admin-ajax.php?action=previewemail&mail=WC_Email_New_Order&order=914

    In this case preview_emails function will be:

    function preview_email()
    {
        $orderId  = $_GET['order'];
        $email_class = $_GET['mail'];
    
        $wc_emails = new WC_Emails();
        $emails = $wc_emails->get_emails();
    
        $new_email = $emails[$email_class];
        $new_email->trigger($orderId);
    
        echo $new_email->get_content();
    
        return null;
    }
    
    add_action('wp_ajax_previewemail', 'preview_email');

    Hi! Thanks for the code.

    Unfortunately I can’t get your code to work… I have the latest version of WooCommerce. I’ve copied your code and pasted it into functions.php and used the url (with my domain). The only thing I get when trying to view the email is the number 0.

    Do you know what’s going wrong?

    Thanks in advance!

    Best regards,
    Jesper

    Nevermind! Silly mistake from me, it’s now working, thank you very much!

    / Jesper

    udarts

    (@udarts)

    Hi Aklesh,

    I tried your code but all I get is a 0 in the browser, I used the second option (the one from the url).

    I use wordpress 4.0.1 and have the email templates copied over to my themes folder.

    Please help.

    Thanks.

    Hi udarts,

    The code should work with the latest WordPress and WooCommerce releases.
    I guess you copied your functions.php to your child-theme?

    The ajax-call will return a 0 if the call can’t be run or can’t be found.
    Please check if your URL is consistent with the function in functions.php and the add_action(‘wp_ajax_previewemail‘, ‘preview_email’).
    here the ‘preview_email’ revers to the function preview_email() and the wp_ajax_previewemail declares the ajax-call previewemail (which you use in your URL).

    If this still doesn’t work for you, please post your code and URL you use.

    I just tried this function but I am not getting the inline styles included in the preview. I know WC just changed how they are injecting styles in 2.3… Does anyone know how to fix?

    Hi Aklesh,

    Sorry for the late reply.

    I tried the code again and works now, but it sends out new emails every time I try it.

    Can we preview the emails without resending the email every time?

    Thanks,
    udarts.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Preview follow-up mail templates’ is closed to new replies.