0

I want to use the custom fields that I can create into the order post in WooCommerce but using placeholders. As you know, into the email settings of WooCommerce, where you can edit the email body, it says:

Optional email body text. You can use the following placeholders: {order_date}, {order_number}, {order_status}, {billing_first_name}, {billing_last_name}, {billing_company}, {blogname}, {site_title}

I want to know the way to create my own placeholders. I'm trying to do this mainly because I want to include the shipping tracking number for my customers.

I found this code in another post:

/**
 * To use: 
 * 1. Add this snippet to your theme's functions.php file
 * 2. Change the meta key names in the snippet
 * 3. Create a custom field in the order post - e.g. key = "Tracking Code" value = abcdefg
 * 4. When next updating the status, or during any other event which emails 
 *    the user, they will see this field in their email
 */
add_filter( 'woocommerce_email_order_meta_keys', 'my_custom_order_meta_keys' );
function my_custom_order_meta_keys( $keys ) {
    $keys[] = 'Tracking Code'; // This will look for a custom field called 'Tracking Code' and add it to emails
    return $keys;
}

I already used it and it works, but the custom field is added to the end of "order details" and is not necessary to add any placeholder, but I want to associate it to a placeholder and in this way to be able to use it in other parts of the email, like the heading, subject or the body, for example, put in the body:

You tracking number is {tracking_code}

PD: I have the Woocommerce Addon "WooCommerce Admin Custom Order Fields" It could help

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    Does this answer your question? [Create additional variables/placeholders for Woocommerce email notifications subject](https://stackoverflow.com/questions/50120643/create-additional-variables-placeholders-for-woocommerce-email-notifications-sub) – 7uc1f3r Oct 06 '20 at 07:16
  • Note that the hook `woocommerce_email_order_meta_keys` is deprecated since WooCommerce 3.0… see for example: [Order custom fields not displayed on WooCommerce email notifications](https://stackoverflow.com/a/61970123/3730754) – LoicTheAztec Oct 06 '20 at 12:05

0 Answers0