WC_Checkout钩子中订单项与订单元数据差异及元显示异常求助
Hey there, let's break down your problem step by step and fix that metadata conflict, plus clarify the difference between order meta and order item meta.
The core issue here boils down to two key points: incorrect hook usage for order item metadata and hook priority conflicts.
First, let's clear up a common mix-up: woocommerce_checkout_update_order_meta is built for adding order-level metadata—not data tied to individual product lines in the order. If you're trying to attach custom info to specific items in the order, this hook isn't the right tool for the job.
The TM Extra Product Options plugin uses woocommerce_checkout_create_order_line_item—which is the correct hook for order item metadata—because it fires as each line item is being created during checkout.
Here's how to adjust your code to resolve the conflict:
- Switch to using
woocommerce_checkout_create_order_line_itemfor your custom order item metadata. - Set a higher priority (like
20) for your hook so it runs after the TM plugin's hook (which likely uses the default priority of10). This ensures your metadata gets added without being overwritten.
Example code snippet:
add_action( 'woocommerce_checkout_create_order_line_item', 'add_my_custom_order_item_meta', 20, 4 ); function add_my_custom_order_item_meta( $item, $cart_item_key, $values, $order ) { // Replace 'your_custom_data_key' with the key from your cart or data source if ( isset( $values['your_custom_data_key'] ) ) { // Add metadata: (label for display, custom value, make visible on frontend) $item->add_meta_data( 'My Custom Option', $values['your_custom_data_key'], true ); } }
Why this works:
- The
$itemparameter gives you direct access to the order line item object, soadd_meta_data()ensures your data is stored correctly in the order item meta table. - The higher priority means your code runs after the TM plugin's hook, so your metadata gets appended instead of being overwritten.
Let's clarify the two types of metadata in WooCommerce checkout hooks:
Order Metadata (Order-Level)
- Purpose: Data attached to the entire order (not individual products). Think of this as global order information.
- Common uses: Invoice numbers, customer special instructions, order-level discounts, or custom fields that apply to the whole order.
- Storage: Stored in the
wp_postmetatable, linked to the order's post ID. - Recommended hook:
woocommerce_checkout_update_order_meta(you were using this hook, but it's for order-level data, not line items). - Display: Shows up in the order details page's "Order Information" section (not under individual product lines).
Order Item Metadata (Line-Level)
- Purpose: Data attached to a specific product line in the order. This is per-product custom data.
- Common uses: Product custom options (like size/color from TM Extra Product Options), add-ons, or personalized text for a specific item.
- Storage: Stored in the
wp_woocommerce_order_itemmetatable, linked to each individual order item ID. - Recommended hooks:
woocommerce_checkout_create_order_line_itemorwoocommerce_add_order_item_meta. - Display: Shows up under the product name on the order completion page, order details page, and customer order emails.
To confirm your metadata is being added correctly:
- Go to WordPress Admin → WooCommerce → Orders.
- Open a test order created after updating your code.
- Scroll down to the "Order Items" section, click the "View" link next to a product.
- You should see your custom metadata listed alongside the TM plugin's metadata.
内容的提问来源于stack exchange,提问作者Rashmi M

