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:So in your
functions.php
you can do this: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:
and then in your header template use it as
$email_id
, eg: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:
Then directly call the specific header template in your email template. For example, in the
customer-invoice.php
template, we can callwc_get_template()
to directly load an appropriate/specific header. Assuming you’ve duplicated theemail-header.php
template and renamed the one for customer invoices toemail-header-invoice.php
it might look like this:My local set up doesn’t email so I’ve tested it with the following:
I am seeing the new header being called by the modified
customer-processing-order.php
template.I could use a condition header using
did_action
function on email-header.php file in order to check which type of email was triggered.You can check what are the actions for each email on their class constructors. Each email have it’s own class file on woocommerce/includes/emails/ folder (i.e. woocommerce/includes/emails/class-wc-email-customer-note.php ). When checking for invoice email and new user emails, which are not triggered by
add_action
, you can check for$order
and$user_login
variables:I did something similar in my plugin https://wordpress.org/plugins/wc-multiple-email-recipients/.
When using
woocommerce_email_headers
you can passID
andHeader
as arguments.