Add extra meta for orders in Woocommerce

Add extra meta for orders in Woocommerce

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Add Extra Meta for Orders in WooCommerce

When extending WooCommerce to store custom data on order items, it is crucial to understand that wp_postmeta and woocommerce_order_item_meta operate on different contexts than standard post meta. Your attempt to hook into woocommerce_before_checkout_process while trying to access the global $item_id reveals two fundamental misunderstandings of how the WooCommerce loop functions.

The primary issue lies in the timing of your hook. The action woocommerce_before_checkout_process fires once at the beginning of the checkout flow, before any items have been processed or added to the cart/order context. At this specific moment, there is no active order item being iterated over. Consequently, the global variable $item_id does not exist because WooCommerce has not yet defined which item it is currently handling. You cannot pass a variable from inside a loop in one function call to another unless that data is explicitly stored or passed as an argument.

In your code snippet, you are passing $item_id and $values to add_item_meta. However, these parameters do not exist within the scope of the woocommerce_before_checkout_process action. WooCommerce does not trigger this hook for every item individually; it triggers it once per checkout process. Therefore, even if you could access a global variable here, it would likely be empty or refer to the cart session rather than a specific order item meta context.

To successfully add custom meta data like _has_event to each line item, you need to hook into the loop where items are actually being processed. The correct approach depends on whether you are adding this meta during the checkout process itself or simply after an order is placed.

If you require the meta data to be available immediately upon adding an item to the cart or validating the checkout form, you must use woocommerce_before_checkout_form combined with a custom loop, or better yet, hook into the specific action that processes each line item during checkout. However, the most robust solution for attaching meta to order items is usually done after the order is created but before it is saved to the database permanently, or by iterating through the cart items directly.

Consider this implementation pattern using woocommerce_after_calculate_totals, which fires once the cart totals are calculated but before the order is placed. This hook allows you to iterate through the cart items and apply your custom logic:

add_action( 'woocommerce_after_calculate_totals', array( $this, 'add_item_meta' ), 10, 0 );

function add_item_meta( $cart ) {
    foreach ( WC()->cart->get_cart() as $item_id => $values ) {
        wc_add_order_item_meta( $item_id, '_has_event', 'yes' );
    }
}

In this example, we manually iterate through the cart using WC()->cart->get_cart(). The loop provides us with both the $item_id and the $values array for each product in the cart. We then call wc_add_order_item_meta() inside this loop. Note that while wc_add_order_item_meta is designed for order items, when called during the checkout process on a cart item ID, it effectively registers the meta against the pending order line item.

A critical distinction exists between cart items and order items. If you are trying to add meta after the order is placed (e.g., in an order_created callback), you must ensure you have access to the $order_id. In that scenario, wc_add_order_item_meta takes a specific order item ID, which can be retrieved by looping through $order->get_items().

It is important to note that relying on global variables like $item_id without knowing exactly which hook fires them is a common pitfall. WooCommerce hooks are event-driven; the data available changes depending on whether you are in the cart context, the checkout form context, or the order processing context. Always inspect the available arguments passed to your callback function rather than guessing at global state.

For more complex scenarios involving custom meta management across orders, checking the LaravelCompany documentation can provide insights into building robust e-commerce extensions, though standard WooCommerce hooks remain the most reliable method for direct database manipulation via wp_postmeta.

Our network