0

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!

0

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:

  1. It's an action, not a filter (because the template has 'do_action' in it)
  2. In this template you should echo or printf 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:

  1. do_action which will call anything that adds an action for '`'woocommerce_email_header''
  2. 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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.