2

Not a duplicate of Display order total with and without VAT in Woocommerce's order email notification.

How can I customize the subject of the E-mail, (not the body. I know how to edit the variables in the body, but the same variables don't work in the subject for some reason).

So I am trying to change the subject of WooCommerce order email notifications.

The subject is customizable via WooCommerce -> Settings -> E-mails -> New Order -> Manage… and according to WooCommerce documentation, there are variables such as {order_date}, {customer_name}, etc... that can be use to get dynamic order data.

My problem is that I would like to show the "order number" and the "order total" in the subject:

  • If I use the format: Order {order_number} - {order_date} - {dollars_spent_order}
  • I should get: Order # 123456 - July 6, 2018 - {dollars_spent_order}

But it doesn't work. Using single or double curly-braces makes no difference.

How can I add the order total (including tax, shipping, etc) to the E-mail subject line?

I would like to get the following result:

Order # 123456 - July 6, 2018 - $ 1,234.56

I Googled this, but didn't find any code snippets that show me how to do this.

There's also a "WooCommerce Follow-Ups" extension that adds more variables, but it's $ 99.00 and I don't need all those, just the order total. There's GOT to be a code snippet somewhere...

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
TomJones999
  • 775
  • 2
  • 13
  • 30
  • Possible duplicate of [Display order total with and without VAT in Woocommerce's order email notification](https://stackoverflow.com/questions/50766161/display-order-total-with-and-without-vat-in-woocommerces-order-email-notificati) – Mohammad Jul 06 '18 at 17:06
  • @Mohammad Sorry but if you pay attention reading the title first and the body of the question, you will see that this question can NOT be a duplicate of the linked answer you suggest *(one of my answers)*. – LoicTheAztec Jul 06 '18 at 23:13
  • **To the community:** Sorry but this question *has a clear statement and include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself.* So please reopen it as the answer is something useful for the community. – LoicTheAztec Jul 11 '18 at 16:24
  • I'd love to know the answer to this, too. It's not a duplicate, email body and subject line are different things. – Aaron Oct 19 '19 at 20:05

1 Answers1

3

To customize for example the "New Order"email subject (sent to the admin), you will use the following hooked function:

add_filter( 'woocommerce_email_subject_new_order', 'custom_email_subject', 20, 2 );
function custom_email_subject( $formated_subject, $order ){
    return sprintf( __('Order # %d - %s - %s', 'woocommerce'), 
        $order->get_id(), // Order ID
        $order->get_date_modified()->date_i18n('F j, Y'), // Formatted date modified
        wc_price($order->get_total()) // Order Total
    );
}

Code goes in function.php file of the active child theme (or active theme). Tested and works.


Now to customize others email notifications (on hold, processing, completed or invoice), you can use any of this filter hooks:

  • woocommerce_email_subject_customer_on_hold_order
  • woocommerce_email_subject_customer_processing_order
  • woocommerce_email_subject_customer_completed_order
  • woocommerce_email_subject_customer_invoice
  • woocommerce_email_subject_customer_note
  • woocommerce_email_subject_low_stock
  • woocommerce_email_subject_no_stock
  • woocommerce_email_subject_backorder
  • woocommerce_email_subject_customer_new_account

Addition:

If you want to remove all html to keep the formatted price without html tags, you can replace:

wc_price($order->get_total())

by (thanks to A K):

html_entity_decode( strip_tags( wc_price( $order->get_total() ) ) );
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 95% of the way there. The variable does appear, but it's surrounded by HTML: Subject: Order # 123456 - July 11, 2018 - $1.00 How do I get rid of the "" ? – TomJones999 Jul 11 '18 at 16:12
  • @TomJones999 You should click on "reopen" link at the bottom of your question, as your question has everything needed in it and is not off-topic. – LoicTheAztec Jul 11 '18 at 16:26
  • I got this to work by changing one line to this: html_entity_decode(strip_tags(wc_price($order->get_total()))) // Order Total – Aaron Oct 19 '19 at 20:23
  • @AK Yes thanks, I have also been using this trick too… I have added that at the end of this thread. – LoicTheAztec Oct 19 '19 at 23:33