For this answer I referred to this article which has a bunch of useful information in it
So there’s a bunch of points here with what you’re trying to do.
Fixes to your code
First, in your second code block, you may be missing the point that this email template works by outputting HTML directly, so the changes to fix this code are:
- It’s an action, not a filter (because the template has ‘do_action’ in it)
- In this template you should
echo
orprintf
stuff directly, not return it.
However, I’m not sure that’s what you want to do, because…
How this template works
The second point is in these templates there is stuff that is ‘hard coded’ in the template, and there is stuff that is ‘hooked’ where it uses actions/filters to get content from outside the template.
In the first code block in your question there are two pieces:
do_action
which will call anything that adds an action for ‘`’woocommerce_email_header”- Some code which always prints ‘You’ve received the following order …’ into the template
How to hook into an action
So, you can hook into the first part by removing the existing action and adding your own, like:
remove_action( 'woocommerce_email_header', 'email_header');
add_action('woocommerce_email_header','jeck_email_header');
function jeck_email_header( ...) {
// print my email header
}
But the second part (the line that starts <p><?php printf( esc_html__( 'You’ve received the following order from %s:'
is hard coded into the admin-new-order.php template, which means if you want to change that content or functionality you have to edit that template file directly. (There may be a hook to search and replace this string out of the template after it’s been built, but this is probably harder than just changing the template file directly)
The actual answer is…
So, unless you really want to hunt down the right hook for changing the content after it comes out of admin-new-order.php, just change the template directly to what you want, i.e. something like:
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
<?php /* translators: %s: Customer billing full name */ ?>
<p><?php printf( esc_html__( 'You’ve received the following order with shipping name %s:', 'woocommerce' ), $order->get_formatted_shipping_full_name() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
Other Notes
It’s also worth noting that I’m not sure what you meant with $foo= <?php printf(...); ?>
. I think probably what you wanted would be $foo = "<p>" . printf( ...) . "</p>";
And anything outsidof <?php ?>
will get output exactly as if you’d echo
‘d it.