tips

How to preview email templates of Woocommerce

Previewing the email templates by placing orders. I think it should be an official option.
I modified the built in WooCommerce functions.

add_filter('woocommerce_mail_content', function( $message ){

  // Email Previewer for WooCommerce
  if ( is_admin() && isset( $_GET['preview_woocommerce_mail'] ) && isset( $_GET['_wpnonce'] ) ) {
    error_reporting(0);
    add_filter( 'wc_get_template', function ( $template, $template_name, $args, $template_path, $default_path ){ // include woocommerce subscription
      if ( isset( $args['email']->template_base ) && !file_exists( $template ) && file_exists( $args['email']->template_base.$template_name ) ){
        $template = $args['email']->template_base.$template_name;
      }
      return $template;
    }, 1, 5);

    $form = '<form method="get" id="preview_email" class="wp-core-ui" action="'.admin_url().'">'."\n";
    $form .= '<input type="hidden" name="preview_woocommerce_mail" value="true">'."\n";
    $form .= '<input type="hidden" name="_wpnonce" value="'.$_GET['_wpnonce'].'">'."\n";
    $form .= '<select id="preview_email_type" name="email" class="regular-text">'."\n";
    $form .= '<option value="">'.__( 'Email', 'woocommerce' ).'</option>'."\n";
    $wc_emails = WC_Emails::instance();
    $emails = $wc_emails->get_emails();
    foreach ( $emails as $key => $val ):
      $form .= '<option value="'.$key.'"'.( isset( $_GET['email'] ) && $_GET['email'] == $key ? ' selected="selected"': '' ).'>'.$val->title.'</option>'."\n";
    endforeach;
    $form .= '</select>'."\n";

    $orders = get_posts( array( 'post_type' => 'shop_order', 'posts_per_page' => 20, 'post_status' => array_keys( wc_get_order_statuses() ) ) );
    $form .= '<select id="preview_email_order" name="order" class="regular-text">'."\n";
    $form .= '<option value="">'.__( 'Order ID', 'woocommerce' ).'</option>'."\n";
    foreach ( $orders as $order ):
      $form .= '<option value="'.$order->ID.'"'.( isset( $_GET['order'] ) && $_GET['order'] == $order->ID ? ' selected="selected"': '' ).'>#'.$order->ID.'</option>'."\n";
    endforeach;
    $form .= '</select>'."\n";
    $form .= '<button id="preview_email_button" type="submit" class="button button-primary" value="preview_woocommerce_mail">'.__('Preview', 'woocommerce').'</button>'."\n";
    $form .= '</form>'."\n";
    $form .= '<style>#preview_email { display:flex; align-items: flex-start; position:fixed; top:8px; left:8px; right:8px; flex-wrap:nowrap; } #preview_email select { font-size:14px; color: #32373c; border:1px solid #7e8993; box-sizing: border-box; box-shadow: none; display:block; border-radius:2px; padding: 0 24px 0 8px; line-height:30px; height:32px; -webkit-appearance: none; background: #fff url(\'data:image/svg+xml;charset=US-ASCII,%3Csvg%20width%3D%2220%22%20height%3D%2220%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Cpath%20d%3D%22M5%206l5%205%205-5%202%201-7%207-7-7%202-1z%22%20fill%3D%22%23555%22%2F%3E%3C%2Fsvg%3E\') no-repeat right 5px top 55%; background-size: auto; background-size: 16px 16px; cursor: pointer; vertical-align: middle; margin-right:4px; } #preview_email_type { max-width:calc( 100% - 200px ); } #preview_email_order { width:96px; } #preview_email_button { background: #007cba; border:1px solid #007cba; color: #fff; text-decoration: none; text-shadow: none; text-decoration: none; font-size: 14px; width:96px; line-height:32px; height: 32px; margin: 0; padding:0 8px; cursor: pointer; -webkit-appearance: none; border-radius:2px; nowrap; display:block; box-sizing: border-box; } </style>'."\n";

    if ( !empty( $_GET['email'] ) && !empty( $_GET['order'] ) ){
      global $email;
      $email = new WC_Email();
      $email = $emails[ $_GET['email'] ];
      $order = wc_get_order( $_GET['order'] );
      if ( $_GET['email'] == 'WC_Email_Customer_Reset_Password' || $_GET['email'] == 'WC_Email_Customer_New_Account' ){
        if ( !empty( $order->get_user_id() ) ){
          $email->object = new WP_User( $order->get_user_id() );
          $email->user_id = $email->object->ID;
          $email->reset_key = 'XXXXXXXXXXXX';
          $email->user_login = stripslashes( $email->object->user_login );
          $email->user_email = stripslashes( $email->object->user_email );
          $email->recipient = $email->user_email;
          $email->password_generated = '';
          $email->set_password_url = '';
        }
      } else {
        $email->object = $order;
        $email->find = array('{order_date}', '{order_number}', '{order_billing_full_name}');
        $email->replace = array( wc_format_datetime( $email->object->get_date_created() ),       $email->object->get_order_number(), $email->object->get_formatted_billing_full_name() );
        $email->recipient = $order->get_billing_email();
      }
      $email->get_subject();
      $args = array(
        'email' => $email,
        'order' => $email->object,
        'email_heading' => $email->get_heading(),
        'additional_content' => $email->get_additional_content(),
        'blogname' => $email->get_blogname(),
        'sent_to_admin' => 'WC_Email_Customer' != substr( $_GET['email'], 0, 17 ),
        'plain_text' => ( 'plain' === $email->get_email_type() ? true : false ),
      );
      if ( isset( $email->user_login ) ){
        $args['user_id'] = $email->user_id;
        $args['user_login'] = $email->user_login;
        $args['reset_key'] = $email->reset_key;
      }
      if ( stripos( $email->template_html, 'subscription' ) !== false || stripos( $email->template_html, 'switch' ) !== false ){
        if ( $subscriptions = wcs_get_subscriptions_for_order( $order->get_id() ) ){
          foreach( $subscriptions as $subscription_id => $subscription_obj ){
            if( $subscription_obj->order->id == $order->get_id() ) {
              break;
            }
          }
          $email->subscription = $args['subscription'] = $subscription_obj;
        } else {
          $subscriptions = wcs_get_subscriptions( array(
            'subscriptions_per_page' => 1,
            'subscription_status' => array('active') // Active subscriptions
          ));
          if ( $subscriptions ){
            $email->subscription = $args['subscription'] = current($subscriptions);
          } else {
            $email->template_html = '';
          }
        }
      }
      ob_start();
      wc_get_template( $email->template_html, $args );
      $message = ob_get_clean();
      if ( 'plain' === $email->get_email_type() ) {
        $message = '<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>'.get_bloginfo( 'name' ).'</title></head><body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0"><div style="white-space:pre-wrap; background-color:white; padding:4em 1em; max-width:1000px; margin:0 auto;">'.$email->get_content().'</div></body></html>';
      }
      $message = $email->style_inline( $message );// wrap the content with the email template and then add styles.
    }
    $message = str_replace( '</body>', $form.'</body>', $message );
  } 
  return $message;
}, 99 );

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

captcha Enter the code