1

I am creating a custom field Tracking_ID on Woocommerce, where the admin will enter the tracking ID as value.

How can i get the value of the created custom field and display it inside the customer-completed-order.php

Also (Optional) i want to show the tracking ID field in the user's track-order order page in his/her my-account.

Woocommerce>order field:

enter image description here

oceanwp-child/woocommerce/emails/customer-completed-order.php

<?php /* translators: %s: Customer first name */ ?>
<p><?php printf( esc_html__( 'Hi %s,', 'woocommerce' ), esc_html( $order->get_billing_first_name() ) ); ?></p>
<?php /* translators: %s: Site title */ ?>
<p><?php printf( esc_html__( 'Your %s order is completed. Your tracking number is: {$fld}', 'woocommerce' ), esc_html( wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) ) ); ?></p>

2 Answers 2

1

The syntax to get your custom field is:

get_post_meta($post_id, $key, $single);

In your context, you should try:

$tracking_id = get_post_meta($order->get_order_number(), 'Tracking_ID', true);

To show you in your example:

<?php /* translators: %s: Site title */ 
$tracking_id = get_post_meta($order->get_order_number(), 'Tracking_ID', true); ?>
<p><?php printf( esc_html__( 'Your %s order is completed. Your tracking number is: %s', 'woocommerce' ), esc_html( wp_specialchars_decode( get_option( 'blogname' ), esc_html($tracking_id) ) ) ); ?></p>
0
0

Based on Melissa's answer, here is what i did and it worked for me.

    <?php
    #Tracking ID

    $tracking_id = get_post_meta($order->get_order_number(), 'Tracking_ID', true);

    if( ! empty($tracking_id) )
    { ?>
<p> <?php
printf( esc_html__( 'Your tracking number is: %s', 'woocommerce' ), esc_html($tracking_id)  );?> 
</p> <?php 
    }
    ?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.