Stimart Stimart - 8 months ago 72
PHP Question

Woocommerce different headers for each email types

I use Woocommerce and I need to change email header according to its type, so that "customer-new-account.php", "customer-processing-order.php", "admin-new-order.php" (and so on)... they must have different header.

I've just copied woocommerce "emails" folder inside my child template and now I need to know how to make code changes.

Any help is appreciate. ;-)
Thanks in advance.


As mentioned in the comments, I don't think there is a way to use conditional logic on the woocommerce_email_header hook. You could go by the $header variable, but it is kind of a long string and it could change.

First, we remove the existing email header:

function so_27400044_remove_email_header(){
    remove_action( 'woocommerce_email_header', array( WC()->mailer(), 'email_header' ) );
add_action( 'init', 'so_27400044_remove_email_header' );

Then directly call the specific header template in your email template. For example, in the customer-invoice.php template, we can call wc_get_template() to directly load an appropriate/specific header. Assuming you've duplicated the email-header.php template and renamed the one for customer invoices to email-header-invoice.php it might look like this:

 * Customer invoice email
 * @author      WooThemes
 * @package     WooCommerce/Templates/Emails
 * @version     2.2.0

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly


<?php do_action( 'woocommerce_email_header', $email_heading ); ?>

<?php wc_get_template( 'emails/email-header-invoice.php', array( 'email_heading' => $email_heading ) ) ; ?>

<?php if ( $order->has_status( 'pending' ) ) : ?>

My local set up doesn't email so I've tested it with the following:

function kia_testing(){ 

$order= wc_get_order( 381 );
        wc_get_template( 'emails/customer-processing-order.php', array(
            'order'         => $order,
            'email_heading' => 'some title',
            'sent_to_admin' => false,
            'plain_text'    => true
        ) );
        echo ob_get_clean();

add_action( 'woocommerce_before_single_product' , 'kia_testing' );

I am seeing the new header being called by the modified customer-processing-order.php template.