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.
I believe the cleanest approach is unbind the default email header action and make your custom one. If you check any of the email templates, eg.
/woocommerce/templates/emails/admin-new-order.php
, you will see at the top that they already pass the email object as a second parameter to the action, just the default WC hooks don't use it:
<?php do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
So in your functions.php
you can do this:
// replace default WC header action with a custom one
add_action( 'init', 'ml_replace_email_header_hook' );
function ml_replace_email_header_hook(){
remove_action( 'woocommerce_email_header', array( WC()->mailer(), 'email_header' ) );
add_action( 'woocommerce_email_header', 'ml_woocommerce_email_header', 10, 2 );
}
// new function that will switch template based on email type
function ml_woocommerce_email_header( $email_heading, $email ) {
// var_dump($email); die; // see what variables you have, $email->id contains type
switch($email->id) {
case 'new_order':
$template = 'emails/email-header-new-order.php';
break;
default:
$template = 'emails/email-header.php';
}
wc_get_template( $template, array( 'email_heading' => $email_heading ) );
}
If you don't need to switch whole file and just want a small change in existing header, you can pass the email type parameter into the template, just replace the bottom template inclusion by:
wc_get_template( $template, array( 'email_heading' => $email_heading, 'email_id' => $email->id ) );
and then in your header template use it as $email_id
, eg:
<?php if($email_id == 'new_order'): ?>
<h2>Your custom subheader to appear on New Order notifications only</h2>
<?php endif ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With