I am using a plugin on my website that generates a shipping label for the customer at checkout. When the shipping label is generated, data such as the tracking number & tracking URL are saved as meta data in the form of an array. The tracking URL provided has our company’s branding, so we want to direct the customer to this URL for tracking. The plugin that we are using to generate the emails sent when the customer places in order does not support pulling meta data that is part of an array. What I would like to do is get the tracking URL from the array and then save it as its own meta data after the shipping label is generated. I would imagine this should be possible, but I cannot figure it out.
I do have a snippet in place that displays the tracking URL on the order page in the admin dashboard. Hopefully this will give someone some insight on how to solve my problem!
// Add tracking link & shipping label link to admin order page
add_action('woocommerce_admin_order_data_after_shipping_address', 'gyta_tracking', 10, 1 );
function gyta_tracking($order){
$tracking = get_post_meta( $order->get_id(), '_wcpti_easypost_tracking' );
$tracking_number = $tracking[0]['tracking_code'];
$tracking_url = $tracking[0]['public_url'];
$print_label = get_post_meta( $order->get_id(), '_wcpti_easypost_postage_label_png_url');
echo '<p><strong>'.__( 'Tracking' ).':</strong> ' . '<a href="' . $tracking_url . '" target="_blank">' . $tracking_number . '</a>' . '</p>';
echo '<p><a href="' . $print_label[0] . '" target="_blank">View Label</a></p>';
}
I have tried getting the same meta data using the above code and then saving the value as it’s own meta data but I believe I am using the incorrect hook.
2
Answers
So I was able to combine code that I found on this link Using update_meta_data on WooCommerce thank you page
Here's the solution I came up with.
This is working perfectly now. Thanks for all the help!
You should not use anymore WordPress post meta functions, getter and setters methods for orders (introduced in WooCommerce 3) are now mandatory since WooCommerce is migrating to custom database tables.
Try the following revised code that is compatible with High-Performance Order Storage (HPOS):
Now for to get this data separated as strings in other custom fields, try:
Code goes in functions.php file of your child theme (or in a plugin). It should work.