Skip to content

Instantly share code, notes, and snippets.

@rbsmidt
Last active May 24, 2017 12:10
Show Gist options
  • Save rbsmidt/1c1c5a43dce77dea8e121f3063e6e4b7 to your computer and use it in GitHub Desktop.
Save rbsmidt/1c1c5a43dce77dea8e121f3063e6e4b7 to your computer and use it in GitHub Desktop.
function wordimpress_preview_woo_emails() {
if ( is_admin() ) {
$default_path = WC()->plugin_path() . '/templates/';
$plugin_path = plugin_dir_path() . '';
$files = scandir( $default_path . 'emails' );
/**
* Add emails from additional plugins by adding to the $additional_paths array.
* A new plugin is added by inserting an array with assoc key 'path' (required) and 'class_dependency' (optional) to the array.
* path: Must point to a valid plugin folder (or sub folder) where the standard 'email'-folder containing custom email templates are located and include beginning and trailing slash.
* class_dependency: Optionally add a class to check for before fetching files. This ensures that the plugin is activated.
* See blow for example (WooCommerce Subscriptions is used for reference):
* array(
* 'path' => '/woocommerce-subscriptions/templates/',
* 'class_dependency' => 'WC_Subscriptions'
* )
*/
$additional_paths = array(
array(
'path' => '/woocommerce-subscriptions/templates/',
'class_dependency' => 'WC_Subscriptions'
)
);
$add_files = [];
if(!empty($additional_paths)){
foreach($additional_paths as $add_path){
if(isset($add_path['class_dependency'])){
if(is_dir(WP_PLUGIN_DIR . $add_path['path']) && class_exists( $add_path['class_dependency'] )){
$add_emails = scandir(WP_PLUGIN_DIR . $add_path['path'] . 'emails');
$add_files = array_merge($add_files, $add_emails);
}
} else {
if(is_dir(WP_PLUGIN_DIR . $add_path['path'])){
$add_emails = scandir(WP_PLUGIN_DIR . $add_path['path'] . 'emails');
$add_files = array_merge($add_files, $add_emails);
}
}
}
$files = array_merge($files, $add_files);
}
$exclude = array(
'.',
'..',
'email-header.php',
'email-footer.php',
'email-styles.php',
'email-order-items.php',
'email-addresses.php',
'email-customer-details.php',
'plain'
);
$list = array_diff( $files, $exclude );
$woocommerce_orders = new WP_Query( array(
'post_type' => 'shop_order',
'posts_per_page' => -1,
'order' => 'ASC',
'post_status' => array( 'wc-processing', 'wc-pending' )
) );
$order_drop_down_array = array();
if( $woocommerce_orders->have_posts() ) {
while( $woocommerce_orders->have_posts() ) {
$woocommerce_orders->the_post();
$order_drop_down_array[get_the_ID()] = get_the_title();
}
}
?>
<div id="template-selector">
<a href="https://wordimpress.com" target="_blank" class="logo"><img src="<?php echo get_stylesheet_directory_uri(); ?>/woocommerce/emails/img/wordimpress-icon.png">
<p>Impressive Plugins, Themes, and more tutorials like this one.<br /><strong>"Press Forward!"</strong>
</p></a>
<form method="get" action="<?php echo site_url(); ?>/wp-admin/admin-ajax.php">
<div class="template-row">
<input id="setorder" type="hidden" name="order" value="">
<input type="hidden" name="action" value="previewemail">
<span class="choose-email">Choose your email template: </span>
<select name="file" id="email-select">
<?php
foreach ( $list as $item ) { ?>
<option value="<?php echo $item; ?>"><?php echo str_replace( '.php', '', $item ); ?></option>
<?php } ?>
</select>
</div>
<div class="order-row">
<span class="choose-order">Choose an order number: </span>
<select id="order" onchange="process1(this)" name="order">
<?php foreach( $order_drop_down_array as $order_id => $order_name ) { ?>
<option value="<?php echo $order_id; ?>" <?php selected( ( ( isset( $_GET['order'] ) ) ? $_GET['order'] : key($order_drop_down_array) ), $order_id ); ?>><?php echo $order_name; ?></option>
<?php } ?>
</select>
</div>
<input type="submit" value="Go">
</form>
</div>
<?php
global $order, $billing_email;
reset( $order_drop_down_array );
$order_number = isset( $_GET['order'] ) ? $_GET['order'] : key( $order_drop_down_array );
$order = new WC_Order( $order_number );
$emails = new WC_Emails();
$email_heading = return_wooc_email_heading( $emails->emails, $_GET['file'], $order_number );
$user_id = (int) $order->post->post_author;
$user_details = get_user_by( 'id', $user_id );
// Load the email header on files that don't include it
if( in_array( $_GET['file'], array( 'email-customer-details.php', 'email-order-details.php' ) ) ) {
wc_get_template( 'emails/email-header.php', array(
'order' => $order,
'email_heading' => $email_heading
) );
}
do_action( 'woocommerce_email_before_order_table', $order, false, false );
wc_get_template( 'emails/' . $_GET['file'], array(
'order' => $order,
'email_heading' => $email_heading,
'sent_to_admin' => false,
'plain_text' => false,
'email' => $user_details->user_email,
'user_login' => $user_details->user_login,
'blogname' => get_bloginfo( 'name' ),
'customer_note' => $order->customer_note,
'partial_refund' => ''
) );
}
wp_die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment