-
-
Save cliffordp/0780d5a8a88a3cbf1dcb7000646fc536 to your computer and use it in GitHub Desktop.
Preview WooCommerce Emails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment