How to access custom order item meta data from a meta key in WooCommerce?


I’m trying to display some custom meta data that’s associated with the shipping on an order in WooCommerce. Here’s the entire story of what I’m doing (so it might help someone else, and so you know fully what I’m trying to do)….

When an order is created, I’m attaching a lead time as meta data to the shipping item of the order. Here’s the code for reference:

add_action( 'woocommerce_checkout_create_order_shipping_item', 'add_leadtime_meta',  20, 4  );

function add_leadtime_meta( &$item, $package_key, $package, $order ) {
    $lt = get_current_lead_time();   
    $item->add_meta_data( 'Lead Time', $lt, true );

When I look at the database, it’s being created, and the order_item_id matches the order_item_id for shipping on that order, so that all is working fine (and I can see it displayed on the backend order display view).

What I’m trying to figure out is how to manually pull this data so I can display it in a few locations around the site (and in e-mail). Where I’m starting with is on the user account orders page (/my-account/orders/). I’ve added a column to display this value, and I’m trying to figure out how to get the data out. Here’s the code for how I added a column in case someone’s curious:

add_filter( 'woocommerce_account_orders_columns', 'new_orders_columns' );
function new_orders_columns( $columns = array() ) {

// Hide the columns
if( isset($columns['order-total']) ) {
    // Unsets the columns which you want to hide
    unset( $columns['order-number'] );
    unset( $columns['order-date'] );
    unset( $columns['order-status'] );
    unset( $columns['order-total'] );
    unset( $columns['order-actions'] );

// Add columns back with new one so we can set the order
$columns['order-number'] = __( 'Invoice', 'Text Domain' );
$columns['order-date'] = __( 'Order Date', 'Text Domain' );
$columns['order-lead-time'] = __( 'Lead Time', 'Text Domain' );
$columns['order-status'] = __( 'Status', 'Text Domain' );
$columns['order-total'] = __( 'Total', 'Text Domain' );
$columns['order-actions'] = __( 'Action', 'Text Domain' );

return $columns;

Where I’m struggling is getting the lead time value I stored above into the table. I’ve tried everything I can think of, and can find online with no help. I can successfully access the arrays, so I know I’m close, but there’s something I’m doing wrong with the search for the ‘Lead Time’ meta data that isn’t working. Here’s an example of what I’ve tried:

add_action( 'woocommerce_my_account_my_orders_column_order-lead-time', 'bs_order_lead_time_data' );
function bs_order_lead_time_data( $order ) {
    foreach( $order->get_items( 'shipping' ) as $item_id => $shipping_item_obj ){
        $shipping_method_id          = $shipping_item_obj->get_method_id(); // The method ID
        $shipping_method_instance_id = $shipping_item_obj->get_instance_id(); // The instance ID

    $lt1 = wc_get_order_item_meta( $shipping_method_id , 'Lead Time', $single = true );
    $lt2 = wc_get_order_item_meta( $shipping_method_instance_id , 'Lead Time', $single = true );
    $lt3 = get_post_meta( $shipping_item_obj, 'Lead Time', true );

    //echo $shipping_method_id;             //this displays 'free_shipping' FYI
    //echo $shipping_method_instance_id;    //this displays '2' FYI
    //echo $lt1;                            //this one displays nothing...
    //echo $lt2;                            //this one displays nothing...
    //echo $lt3;                            //this one displays nothing...
    echo $shipping_item_obj;                //doing this shows me the data object and the content I need is there.


Using the last example of the echo, here’s the data array I’m getting:

{"id":39,"order_id":2050,"name":"Free shipping","method_title":"Free shipping","method_id":"free_shipping","instance_id":"2","total":"0.00","total_tax":"0","taxes":{"total":[]},"meta_data":[{"id":573,"key":"Items","value":"Test Part × 1"},{"id":574,"key":"Lead Time","value":"2 to 3 weeks"}]}

I’m sure I’m missing something simple, but I can’t seem to find it. Any help would be appreciated.



, Danny 4 years 2020-02-25T08:38:39-05:00 0 Answers 103 views 0

Leave an answer