Fost
Fost

Reputation: 11

Display a custom field value in Woocommerce email notification template

In WooCommerce, I am using "WC Fields Factory" plugin to create a 'serial' custom field. I need to display its value in /woocommerce/emails/customer-completed-order.php template file.

I have tried to use:

echo get_post_meta(get_post()->ID, "wccaf_serial", true );

But it does not work.

What I am doing wrong?

Thanks

Upvotes: 1

Views: 2504

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 253773

You can use directly $order with all WC_Order methods in this email template, to get the Order ID, this way:

// Get the Order ID (WooCommerce retro-compatibility)
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

// Get "serial" custom field value
$serial = get_post_meta($order_id, "wccaf_serial", true );

// Display "serial" custom field value
echo '<p>'.__('Serial', 'woocommerce') . $serial . '</p>';

Please read: Template Structure + Overriding Templates via a Theme


Also instead of overriding this template, you can use any available hook for example like:

add_action( 'woocommerce_email_order_details', 'action_wc_email_order_details' 50, 4 );
function action_wc_email_order_details( $order, $sent_to_admin, $plain_text, $email ){
    // Get the Order ID (WooCommerce retro-compatibility)
    $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;

    // Get "serial" custom field value
    $serial = get_post_meta($order_id, "wccaf_serial", true );

    // Display "serial" custom field value
    echo '<p>'.__('Serial', 'woocommerce') . $serial . '</p>';
}

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

The code is tested and works in WooCommerce 2.6.x or 3+

Upvotes: 1

Hannes
Hannes

Reputation: 155

Have a look at WP HTML Mail. This plugin has an integrated mailbuilder for WooCommerce emails. You can edit the content of your mail in WordPress editor and add custom fields from menu "Placeholder". Here are some screenshots: http://wp-html-mail.com/woocommerce-custom-email-content/

Upvotes: 0

Related Questions