Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Preview WooCommerce Emails
<?php
/**
* Quick hack to preview WooCommerce e-mails.
* Based on drrobotnik's answer from Stack Overflow: http://stackoverflow.com/a/27072101/186136
*
* Add this to <yourtheme>/functions.php and then visit a url like:
* http://<site_url>/wp-admin/admin-ajax.php?action=previewemail
*
* @return null
*/
function mytheme_preview_email() {
global $woocommerce;
if ( ! is_admin() ) {
return null;
}
$mailer = $woocommerce->mailer();
$email_options = array();
foreach ( $mailer->emails as $key => $obj ) {
$email_options[$key] = $obj->title;
}
$in_order_id = isset( $_GET['order'] ) ? $_GET['order'] : '';
$in_email_type = isset( $_GET['email_type'] ) ? $_GET['email_type'] : '';
$order_number = is_numeric( $in_order_id ) ? (int) $in_order_id : '';
$email_class = isset( $email_options[ $in_email_type ] ) ? $in_email_type : '';
$order = $order_number ? wc_get_order( $order_number ) : false;
$error = '';
$email_html = '';
if ( ! $in_order_id && ! $in_email_type ) {
$error = '<p>Please select an email type and enter an order #</p>';
} elseif ( ! $email_class ) {
$error = '<p>Bad email type</p>';
} elseif ( ! $order ) {
$error = '<p>Bad order #</p>';
} else {
$email = $mailer->emails[$email_class];
$email->object = $order;
$email_html = apply_filters( 'woocommerce_mail_content', $email->style_inline( $email->get_content_html() ) );
}
?>
<!DOCTYPE HTML>
<html>
<head></head>
<body>
<form method="get" action="<?php echo site_url(); ?>/wp-admin/admin-ajax.php">
<input type="hidden" name="action" value="previewemail">
<select name="email_type">
<option value="--">Email Type</option>
<?php
foreach( $email_options as $class => $label ){
if ( $email_class && $class == $email_class ) {
$selected = 'selected';
} else {
$selected = '';
}
?>
<option value="<?php echo $class; ?>" <?php echo $selected; ?> ><?php echo $label; ?></option>
<?php } ?>
</select>
<input type="text" name="order" value="<?php echo $order_number; ?>" placeholder="order #">
<input type="submit" value="Go">
</form>
<?php
if ( $error ) {
echo "<div class='error'>$error</div>";
} else {
echo $email_html;
}
?>
</body>
</html>
<?php
return null;
}
add_action('wp_ajax_previewemail', 'mytheme_preview_email');
@inspiredearth

This comment has been minimized.

Copy link

@inspiredearth inspiredearth commented Aug 22, 2015

Thanks for providing this. I am trying to implement it on WooCommerce v2.4.5 on WP 4.3
When I select a template and type in an order number, it always gives me the error:
Bad order #
Have you tested if it needs any changes for the latest version of WC?

@jnicol

This comment has been minimized.

Copy link

@jnicol jnicol commented Sep 9, 2015

@jonathannz The order # must be the WordPress post ID, not the WooCommerce order number. Navigate to your order in WordPress admin and look for the post ID in the address bar.

@zlove Thank you for this gist! Works perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment