WooCommerce感谢页订单项元数据处理咨询:创建可用短码或调整展示位置
Great questions! Let's tackle them one by one with practical, working solutions:
1. Can we create a working shortcode based on wc_get_order_item_meta?
Absolutely, but you can't directly bind wc_get_order_item_meta as a shortcode function—it requires specific parameters (like the order item ID and meta key) that aren't automatically passed to a shortcode in this context. Instead, we need to build a wrapper function that handles shortcode arguments, validates inputs, and calls wc_get_order_item_meta correctly.
Here's a robust implementation:
// Custom shortcode to fetch order item metadata add_shortcode('custom_order_meta', 'custom_get_order_item_meta_shortcode'); function custom_get_order_item_meta_shortcode($atts) { // Set default shortcode parameters $atts = shortcode_atts(array( 'item_id' => '', 'meta_key' => '', // Optional: specify a single meta key to fetch 'show_key' => true, // Toggle whether to display the meta key label ), $atts); // Exit if no valid order item ID is provided if (empty($atts['item_id']) || !is_numeric($atts['item_id'])) { return ''; } $item_id = intval($atts['item_id']); $meta_output = ''; // Fetch a specific meta key if provided if (!empty($atts['meta_key'])) { $meta_value = wc_get_order_item_meta($item_id, $atts['meta_key'], true); if (!empty($meta_value)) { $meta_output .= '<div class="meta-item">'; if ($atts['show_key']) { $meta_output .= '<span class="meta-label">' . esc_html($atts['meta_key']) . ': </span>'; } $meta_output .= '<span class="meta-value">' . esc_html($meta_value) . '</span></div>'; } } // Fetch all non-hidden order item metadata (skip WooCommerce's default prefixed keys) else { $all_meta = wc_get_order_item_meta($item_id); foreach ($all_meta as $key => $value) { // Skip system meta keys that start with an underscore if (str_starts_with($key, '_')) { continue; } $meta_output .= '<div class="meta-item">'; $meta_output .= '<span class="meta-label">' . esc_html($key) . ': </span>'; $meta_output .= '<span class="meta-value">' . esc_html($value) . '</span></div>'; } } return $meta_output; }
This shortcode gives you flexibility: you can fetch a single meta field by adding meta_key="your_key" to the shortcode, or show all public metadata by default.
2. Moving order item metadata into the product-content div
This is straightforward once we have the working shortcode. We just need to pass the current order item ID to the shortcode when we call it inside your custom_product_on_thankyou function. Here's the updated version of your existing code:
// Style order table with product images and metadata on thank you page add_filter('woocommerce_order_item_name', 'custom_product_on_thankyou', 10, 3); function custom_product_on_thankyou($name, $item, $visible) { // Only run on the order received (thank you) page if (!is_order_received_page()) { return $name; } $product_id = $item->get_product_id(); $_product = wc_get_product($product_id); $thumbnail = $_product->get_image(); // Call our custom shortcode and pass the current order item ID $order_meta_html = do_shortcode('[custom_order_meta item_id="' . $item->get_id() . '"]'); $thk_product = '<div class="checkout-product-wrapper"> <div class="product-image">' . $thumbnail . '</div> <div class="product-content"> <div class="product-name">' . $name . '</div> <div class="product-meta">' . $order_meta_html . '</div> </div> </div>'; return $thk_product; }
Now the order item metadata will render directly inside the product-content div, right below the product name.
Why your original shortcode didn't work
Your initial attempt add_shortcode('custom_order_meta', 'wc_get_order_item_meta'); failed because wc_get_order_item_meta expects at least two arguments ($item_id and $meta_key), but shortcodes pass arguments as an array of attributes—there's no automatic mapping between the shortcode's input and the function's required parameters. The wrapper function fixes this by translating shortcode attributes into the format wc_get_order_item_meta needs.
内容的提问来源于stack exchange,提问作者alench

