问题
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...
回答1:
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() ) ) );
来源:https://stackoverflow.com/questions/51214789/customizing-email-subject-with-dynamic-data-in-woocommerce