wc_get_order_item_meta() Function: Read Item Meta [Guide]

wc_get_order_item_meta function reading order item meta

Every product line, shipping method, fee, and coupon on a WooCommerce order is an order item, and each item stores its own meta. Plugins use that meta for gift notes, chosen options, or booking dates. To read it, you need wc_get_order_item_meta().

This guide covers the function’s parameters, how the $single flag changes the return value, and how to read and write item meta while you loop through an order.

What Order Item Meta Is

Order items are rows that belong to an order. Each row has an ID and its own key/value meta, the same way posts store post meta. Load an order with wc_get_order(), and get_items() returns the items keyed by their item IDs. Those IDs are what the meta functions expect.

If you are new to the order object itself, the WC_Order object guide covers the getters you will use alongside item meta.

Custom Fields box on an order showing my_gift_note set to Happy birthday, Jane!

Tip: If you already hold the item object, call $item->get_meta( 'my_gift_note' ) instead. Use wc_get_order_item_meta() when you only have the item ID, such as a value passed in a request or stored in your own table.

Function Syntax

The function takes three arguments and returns the stored value as mixed:

<?php

wc_get_order_item_meta( $item_id, $key, $single = true );
ParameterTypeDefaultDescription
$item_idintRequiredID of the order item
$keystringRequiredMeta key to read
$singlebooltruetrue returns one value. false returns an array of every stored value.

Read Item Meta

Pass the item ID and the meta key. The third argument is optional.

Get a Single Value

The default $single value of true returns the first value stored under the key. On a shipping item, method_id holds the shipping method identifier, such as flat_rate:

<?php

// Shipping method ID stored on order item 42.
$method_id = wc_get_order_item_meta( 42, 'method_id', true );

Get Repeated Values

A key can hold several values when you save it more than once. Set $single to false to get all of them as an array:

<?php

// Every value stored under this key on order item 42.
$colors = wc_get_order_item_meta( 42, 'my_selected_color', false );

if ( ! empty( $colors ) ) {
    echo esc_html( implode( ', ', $colors ) );
}

Read Meta in an Order Loop

This is the most common pattern. Load the order, loop over its items, and print the note the customer saved on each line. Replace 1234 with a real order ID:

<?php

$order = wc_get_order( 1234 );

foreach ( $order->get_items() as $item_id => $item ) {
    $gift_note = wc_get_order_item_meta( $item_id, 'my_gift_note', true );

    if ( '' !== $gift_note ) {
        echo esc_html( $item->get_name() ) . ': ' . esc_html( $gift_note ) . "\n";
    }
}

Write Item Meta

The write counterpart is wc_update_order_item_meta(), which takes the item ID, the key, and the value. When the line item is built during checkout, you can also attach the meta to the item object. This example assumes the cart item carries my_gift_note:

<?php

add_action( 'woocommerce_checkout_create_order_line_item', 'my_save_gift_note', 10, 4 );

function my_save_gift_note( $item, $cart_item_key, $values, $order ) {
    if ( ! empty( $values['my_gift_note'] ) ) {
        $item->add_meta_data( 'my_gift_note', sanitize_text_field( $values['my_gift_note'] ) );
    }
}

Common Mistakes

Three mistakes account for most broken item meta lookups:

  • Passing the order ID. The first argument is the item ID from get_items(). The order number shown in the admin is a different value.
  • Leaving $single as true for repeated keys. Only the first value comes back, and the rest are silently ignored.
  • Printing raw values. Escape output with esc_html() before you print it, as the examples do.

FAQs About wc_get_order_item_meta()

Is It the Same as get_metadata()?

Not exactly. The function loads WooCommerce’s order-item data store and calls that store’s get_metadata() method. It stays inside WooCommerce’s data layer, so use it instead of the core WordPress meta functions for order items.

Does It Work With HPOS?

Yes. High-Performance Order Storage moves order records into custom tables, but order items and their meta stay in the item tables. Read the HPOS guide for the wider storage picture.

What Does It Return for a Missing Key?

With $single set to true, it returns an empty value. With false, it returns an empty array. Check for both before you use the result.

Conclusion

wc_get_order_item_meta() reads the meta stored on each order line, shipping line, or fee. Pair it with get_items() when you need every line, and use wc_update_order_item_meta() or add_meta_data() when you need to write it.

Need custom order data on your store? Hire a WooCommerce developer, or reach out to me directly.

About The Author

Picture of Andriy Haydash

Andriy Haydash

Andriy Haydash is a WordPress Expert who helps people build and launch successful WordPress membership and e-learning websites.

Note: Not all of the articles are written directly by me.
Affiliate Disclaimer: Some links in the post may be my affiliate links

Kinsta - Managed Hosting for WordPress

Before YOU Leave...
Join My Newsletter

Get practical tips & tricks on how to start, grow and market your course/membership site.