How Do I Add Custom Data to a WooCommerce Order?

How to Add and Retrieve WooCommerce Order Meta

The order object's update_meta_data() and get_meta() methods provide the standard way to attach and read arbitrary custom data, and this is the same underlying mechanism a custom checkout field's value would typically be saved to when captured at checkout.

// Saving custom data to an order.
$order = wc_get_order( $order_id );
$order->update_meta_data( '_gift_message', $gift_message_value );
$order->save();

// Reading it back later.
$gift_message = $order->get_meta( '_gift_message' );

Things Worth Getting Right

  • A leading underscore in the meta key (like _gift_message) hides it from the default custom fields display in the classic order editor, which is usually desired for internal/system data rather than something meant to be manually visible/editable there.
  • Always call save() after update_meta_data(), since the change isn't persisted to the database until explicitly saved.
  • Displaying custom meta to the admin on the order screen (rather than leaving it invisible) needs a separate hook adding it to the order admin display, if that visibility is wanted.
  • HPOS compatibility is maintained automatically through these standard order object methods, since they're the correct, forward-compatible way to interact with order data regardless of the underlying storage system.

Implementing This Properly

  1. Use the order object's meta methods (update_meta_data()/get_meta()) rather than direct database queries, ensuring compatibility with both legacy and HPOS storage.
  2. Decide whether the data should be visible in admin and add appropriate display hooks if so.
  3. Test that the data persists and displays correctly everywhere the order is referenced (admin, emails, My Account, if relevant).

Need custom order data captured and displayed properly across your store? See custom WooCommerce development.

Available for new projects

NEXT STEP

Rather have it fixed than keep reading?

Based in Dhaka, Bangladesh — serving clients worldwide. Tell me what's happening and you'll have a reply within one working day.

  • Reply within one working day
  • Fixed quote before work starts
  • UK, EU and US hours covered