Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Woocommerce - email change header title WordPress

  • SOLVED

Hi!

I am using the latest version of Woocommerce.

My goal is to use some code in the functions.php to change the email header on the typ of the email.
I see that the woocommerce-templates has an action "do_action( 'woocommerce_email_header', $email_heading, $email ); ?>" which can be called.

all my infos to make an decision are in the $email. But I dont unterstand how to use the action to set a new headertext for the email.

Could some provide me some code where i use the functions.php to change an email header without modfiy templates? Is my implementation possibly?

Thanks,
steve

Answers (2)

2016-11-06

Reigel Gallarde answers:

what you are trying to do can be done... but what you're planing is not good.

that action is just calling this wc_get_template( 'emails/email-header.php', array( 'email_heading' => $email_heading ) );

and email-header.php has the needed html mark up.. overwriting this on your functions.php is not good and will create headaches in the future. Also, you might break the html mark up.

here's a sample of email-header.php https://github.com/woocommerce/woocommerce/blob/291c731b22ec83dc18a47a1f2320819ae4cc2a74/templates/emails/email-header.php

This template can be overridden by copying it to yourtheme/woocommerce/emails/email-header.php
and do your edits in that newly created file....

however, adding hooks to wc_get_template can also be done in your functions.php in order to replace email-header.php without creating the file.. but again, it's kind of a pain...


stiffler comments:

no, i only want to change the text what is passed by $email_heading. No html tags or markups.
In your example the output from line 49.

wc_get_template( 'emails/email-header.php', array( 'email_heading' => $email_heading ) );
calls email-header.php with the variable $email_heading which cotains an string. is this true?

and i want to change the $email_heading strings content with the action "do_action( 'woocommerce_email_header', $email_heading, $email );"


Reigel Gallarde comments:

ok... so if you're only editing the heading text, then this can be done in your functions.php...

In general, it looks like this...
function filter_woocommerce_email_heading( $email_heading , $email ) {
// make filter magic happen here...
// making $email_heading = 'New generated heading text'
return $email_heading;
};

// add the filter
add_filter( "woocommerce_email_heading_{$id}", 'filter_woocommerce_email_heading', 10, 2 );

where you need to replace {$id} with the template id... example for new account, id is customer_new_account... so the filter looks like add_filter( "woocommerce_email_heading_woocommerce_email_heading_", 'filter_woocommerce_email_heading', 10, 2 );

to get this id, you have to check the files located in woocommerce/includes/emails
should be inside public function __construct() { } of each file...

and few times, you might see that code above will not work for some email templates... this is because they have their own implementation of get_heading()... in these cases, you can look for this function and look at the filter function inside... for example, inside woocommerce/includes/emails folder is a file class-wc-email-customer-invoice.php... if you take a look at this, it has it's own get_heading() function... other files do not...

the function is like

public function get_heading() {
if ( $this->object->has_status( array( 'completed', 'processing' ) ) ) {
return apply_filters( 'woocommerce_email_heading_customer_invoice_paid', $this->format_string( $this->heading_paid ), $this->object );
} else {
return apply_filters( 'woocommerce_email_heading_customer_invoice', $this->format_string( $this->heading ), $this->object );
}
}


for this you can create another filter like the filter_woocommerce_email_heading...

function filter_woocommerce_email_heading_customer_invoice( $email_heading , $email ) {
// make filter magic happen here...
// making $email_heading = 'New generated heading text for customer invoice'
return $email_heading;
};

// add the filter
add_filter( "woocommerce_email_heading_customer_invoice", 'filter_woocommerce_email_heading_customer_invoice', 10, 2 );

function filter_woocommerce_email_heading_customer_invoice_paid( $email_heading , $email ) {
// make filter magic happen here...
// making $email_heading = 'New generated heading text for customer invoice which is paid'
return $email_heading;
};

// add the filter
add_filter( "woocommerce_email_heading_customer_invoice_paid", 'filter_woocommerce_email_heading_customer_invoice_paid', 10, 2 );


Reigel Gallarde comments:

typo error above

so the filter looks like add_filter( "woocommerce_email_heading_woocommerce_email_heading_customer_new_account", 'filter_woocommerce_email_heading', 10, 2 );


Reigel Gallarde comments:

I hate it when I can't edit my typo errors.. sorry..

add_filter( "woocommerce_email_heading_customer_new_account", 'filter_woocommerce_email_heading', 10, 2 );


Reigel Gallarde comments:

I'd like to mention that you don't need to create multiple functions for the given {$id}

function filter_woocommerce_email_heading( $email_heading , $email ) {
// make filter magic happen here...
// making $email_heading = 'New generated heading text'
return $email_heading;
};

// add the filter
add_filter( "woocommerce_email_heading_customer_new_account", 'filter_woocommerce_email_heading', 10, 2 );
add_filter( "woocommerce_email_heading_cancelled_order", 'filter_woocommerce_email_heading', 10, 2 );
add_filter( "woocommerce_email_heading_customer_completed_order", 'filter_woocommerce_email_heading', 10, 2 );

this will already cover the 3 templates... assuming, they all do the same manipulation... or you can do ifs using $email object... like if ('customer_completed_order' === $email->id) {$email_heading = 'Cool heading'; }


stiffler comments:

AMAZING!

Works like a charme.

Thanks
steve

2016-11-06

mod mi answers:

Easiest way is to copy the email-header.php from the plugins folder to your 'woo commerce' theme folder. If you are not overriding templates you make a folder named 'woo commerce' in your theme's folder then copy the appropriate file there (preserving the folder structured) inside the appropriate subfolder. In your case you should have in your theme's folder a folder "woocommerce" with an "emails" subfolder containing a copy of "email-header.php" file from the plugin's folder. There you can do any changes you need and the plugin will use your template to display the email header.


stiffler comments:

Thanks for the answer. I know the solution to override the templates in my child theme but if woocommerce make an update to the templates i should copy the templates again to get these updates. To avoid this I want a solution over the functions.php like the solution from Reigel Gallarde