5

I have custom statuses and custom emails set up in Woocommerce. I would like to use the current email, WC_Email, not the current status as a variable inside email templates.

I need to have some if statements in the email templates. I am not using the order status to ensure if an email from an order gets resent manually it doesn't send data for the current order status with an separate email.

How can I echo the WC_Email email ID as a variable in Woocommerce?

LoicTheAztec
  • 207,510
  • 22
  • 296
  • 340
logrexton
  • 171
  • 1
  • 11

1 Answers1

13

The wc_order_email class or function doesn't exist in WooCommerce, so I have updated your question.

What you are looking at is $email variable argument (the WC_Email current type object). It's mostly defined everywhere in templates and hooks.

To get the usable current Email ID as a variable you will simply use $email_id = $email->id

To get the current Email ID of your custom emails, you should use this code (just for testing):

add_action( 'woocommerce_email_order_details', 'get_the_wc_email_id', 9, 4 );
function get_the_wc_email_id( $order, $sent_to_admin, $plain_text, $email ) {
    // Will output the email id for the current notification
    echo '<pre>'; print_r($email->id); echo '</pre>'; 
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

You will get the following:

  • new_order
  • customer_on_hold_order
  • customer_processing_order
  • customer_completed_order
  • customer_refunded_order
  • customer_partially_refunded_order
  • cancelled_order
  • failed_order
  • customer_reset_password
  • customer_invoice
  • customer_new_account
  • customer_note

Once you get the correct email ID slug for your custom email notification you can use it on any following hook (instead of overriding email templates):

woocommerce_email_header (2 arguments: $email_heading, $email)
woocommerce_email_order_details (4 arguments: $order, $sent_to_admin, $plain_text, $email)
woocommerce_email_order_meta (4 arguments: $order, $sent_to_admin, $plain_text, $email)
woocommerce_email_customer_details (4 arguments: $order, $sent_to_admin, $plain_text, $email)
woocommerce_email_footer (1 argument: $email)

HERE an example of code where I target "New order" email notifications only:

add_action( 'woocommerce_email_order_details', 'add_custom_text_to_new_order_email', 10, 4 );
function add_custom_text_to_new_order_email( $order, $sent_to_admin, $plain_text, $email ) {
    // Only for "New Order"  email notifications (to be replaced by yours)
    if( ! ( 'new_order' == $email->id ) ) return;

    // Display a custom text (for example)
    echo '<p>'.__('My custom text').'</p>';
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works.

LoicTheAztec
  • 207,510
  • 22
  • 296
  • 340
  • Thanks! I was able to get a variation of this to work. – logrexton Dec 20 '17 at 22:18
  • Adding the code in the first part of your answer, to test for the ID, doesn't work for me: `Fatal error: Uncaught ArgumentCountError: Too few arguments to function add_action(), 1 passed in /home/*****/public_html/framework/wp-content/themes/sgone/woocommerce/emails/email-header.php on line 70 and at least 2 expected` In fact no matter which hook I'm trying to use, this is the cause and $email is an undefined variable. – Skovsgaard Sep 13 '18 at 13:13
  • It seems like I fixed the error now, and your code actually does work. It just doesn't work for email templates for WooCommerce Subscription. That's why. Is there a way to make that work as well? – Skovsgaard Sep 13 '18 at 13:19
  • 1
    @Skovsgaard Tested again … The first function to test the email ID work just perfectly without any error. Now for WooCommerce Subscription it's another thing as it doesn't use the same hook function arguments… – LoicTheAztec Sep 13 '18 at 13:20
  • Yes, my bad. I got it working for WooCommerce Subscription emails now. I did a minor mistake in my code. Sorry. :) – Skovsgaard Sep 13 '18 at 13:22