1

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.

Thanks,

Danny

0

There is at least 2 ways to get custom order item meta data from a defined meta key:
1) Since WooCommerce 3 - Using WC_Data method get_meta()
2) The older way - Using wc_get_order_item_meta() WooCommerce function

So your hooked function bs_order_lead_time_data() code will be:

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 => $item ) {
        // Using WC_Data method get_meta() since WooCommerce 3
        echo $item->get_meta('Lead Time');

        // Or the older way (using always the item Id)
        // echo wc_get_order_item_meta( $item_id , 'Lead Time', true );
    }
}

Code goes in functions.php file of your active child theme (or active theme). It should better work.

  • Worked great. Thanks!! – Danny Sep 23 '19 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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