1

I am using WooCommerce Admin Custom Order Fields plugin to create a custom field for tracking number. I was using the following code to create a shortcode that I was including to order completed email notification:

// [tracking_number]
function tracking_number_func( $atts ){
    global $post;
    $order_id2 = $post->ID;
    return get_post_meta( $order_id2, '_wc_acof_2', true );
    // https://docs.woocommerce.com/document/woocommerce-admin-custom-order-fields/
}
add_shortcode( 'tracking_number', 'tracking_number_func' );

But it's not working now after updating WooCommerce. I am not sure which version change made it un-useable.

What do I need to change to make it work?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
dunkaroo
  • 156
  • 7

1 Answers1

1

There is no post object for email notifications. Depending on where you are using this shortcode, try:

function wc_get_tracking_number( $atts ){
    global $order;

    return get_post_meta( $order->get_id(), '_wc_acof_2', true );
}
add_shortcode( 'tracking_number', 'wc_get_tracking_number' );

// USAGE: [tracking_number]

Code goes in functions.php file of your active child theme (or active theme). Untested, it could work.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • I tried your code and it did not work sadly. After I update the order to complete it went to blank screen to URL "https://myURL.com/wp-admin/post.php" and the email didn't get send out. I found this answer https://stackoverflow.com/questions/21633120/how-can-i-get-the-order-id-in-woocommerce but by replacing `$order_id2 = $post->ID;` from my own code to `$order->get_id();` didn't work either. same blank page and no email get send out. BTW thank you for taking your time – dunkaroo May 08 '19 at 08:58
  • @dunkaroo This code can't work with your plugin… It only works through normal related hooks and related WooCommerce templates. – LoicTheAztec May 08 '19 at 12:06
  • The code did work before the update, I was wondering if the API get changed or what not. I will do some more experiment and googling. If I found a answer I will post back here for people might have a similar problem. Thanks :) – dunkaroo May 08 '19 at 20:06