TransWikia.com

How to incluide custom field in WooCommerce email using placeholder

WordPress Development Asked by Santiago Cuartas Rmrz on December 8, 2020

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}

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP