How to preview email templates of Woocommerce
I ended up creating a small function which is executed via the admin-ajax.php script, e.g.
https://example.org/wp-admin/admin-ajax.php?action=previewemail&file=emails/customer-processing-order.php&order=180
The function:
- sets the global
$order
variable to the order with the id specified in theorder
parameter - load the email template specified in the
file
parameter.
This is the code (you must add it a new plug-in or in some existing php):
/** * Open a preview e-mail. * * @return null */function preview_email(){ global $order; $filename = $_GET['file']; $orderId = $_GET['order']; $order = new WC_Order($orderId); include $filename; return null;}add_action('wp_ajax_previewemail', 'preview_email');
Modified version of the above answer using built in woo functions. The benefit is that it will look in both your theme and the default plugins template path.
/** * Open a preview e-mail. * * @return null */function previewEmail() { if (is_admin()) { $default_path = WC()->plugin_path() . '/templates/'; $files = scandir($default_path . 'emails'); $exclude = array( '.', '..', 'email-header.php', 'email-footer.php','plain' ); $list = array_diff($files,$exclude); ?><form method="get" action="<?php echo site_url(); ?>/wp-admin/admin-ajax.php"><input type="hidden" name="order" value="2055"><input type="hidden" name="action" value="previewemail"> <select name="file"> <?php foreach( $list as $item ){ ?> <option value="<?php echo $item; ?>"><?php echo str_replace('.php', '', $item); ?></option> <?php } ?> </select><input type="submit" value="Go"></form><?php global $order; $order = new WC_Order($_GET['order']); wc_get_template( 'emails/email-header.php', array( 'order' => $order ) ); wc_get_template( 'emails/'.$_GET['file'], array( 'order' => $order ) ); wc_get_template( 'emails/email-footer.php', array( 'order' => $order ) ); } return null; }add_action('wp_ajax_previewemail', 'previewEmail');
I've come up with a solution(plugin) that may fit your needs, although it only works with the default available email-templates this is due to the nature of how WooCommerce is managing emails. Github