admin管理员组文章数量:1122832
I hired a contractor to help me with my site a few years ago when I didn't have the coding experience that I do today. The contractor ended up making new templates for almost every email, and they are all out of date. I started making updates to them when I realized I should just be making the custom changes in the functions.php page so I don't have to go through updating a billion templates each time WooCommerce makes an update.
I'm taking baby steps here and the first thing I'd like to do is for the admin-new-order.php email template.
Towards the top of template there is a section that has this information in it.
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 from %s:', 'woocommerce' ), $order->get_formatted_billing_full_name() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
What I'd like to do is add something to my functions file so that I can tweak this code block (I need it to be shipping instead of billing name). So I was thinking something like this.
add_filter('woocommerce_email_header', 'filter_heading_new_order');
function filter_heading_new_order($example) {
$example = <?php /* translators: %s: Customer billing full name */ ?>
<p><?php printf( esc_html__( 'You’ve received the following order from %s:', 'woocommerce' ), $order->get_formatted_billing_full_name() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
return $example;
}
Disregard syntax above I merely copy/pasted that in to give a feel for what I'm looking for. Regardless of if I were to fix the syntax above it doesn't work. I still see the same stuff in the email template.
My question is how can I use a hook and or filter to replace some of the content of the email template? Specifically the "You’ve received the following order from UserXYZ:" part.
Thanks!
I hired a contractor to help me with my site a few years ago when I didn't have the coding experience that I do today. The contractor ended up making new templates for almost every email, and they are all out of date. I started making updates to them when I realized I should just be making the custom changes in the functions.php page so I don't have to go through updating a billion templates each time WooCommerce makes an update.
I'm taking baby steps here and the first thing I'd like to do is for the admin-new-order.php email template.
Towards the top of template there is a section that has this information in it.
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 from %s:', 'woocommerce' ), $order->get_formatted_billing_full_name() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
What I'd like to do is add something to my functions file so that I can tweak this code block (I need it to be shipping instead of billing name). So I was thinking something like this.
add_filter('woocommerce_email_header', 'filter_heading_new_order');
function filter_heading_new_order($example) {
$example = <?php /* translators: %s: Customer billing full name */ ?>
<p><?php printf( esc_html__( 'You’ve received the following order from %s:', 'woocommerce' ), $order->get_formatted_billing_full_name() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
return $example;
}
Disregard syntax above I merely copy/pasted that in to give a feel for what I'm looking for. Regardless of if I were to fix the syntax above it doesn't work. I still see the same stuff in the email template.
My question is how can I use a hook and or filter to replace some of the content of the email template? Specifically the "You’ve received the following order from UserXYZ:" part.
Thanks!
Share Improve this question edited Jul 5, 2020 at 20:42 J3ck asked Jul 5, 2020 at 20:21 J3ckJ3ck 115 bronze badges1 Answer
Reset to default 0For 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.
本文标签: Would like to use hook or filters to customize email templates
版权声明:本文标题:Would like to use hook or filters to customize email templates 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736293624a1929184.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论