如何通过Admin Columns Pro在WooCommerce订单列展示Events订单项元数据
Solution: Display Events Category Order Item Meta in Admin Columns Pro
Got it, let's tweak your existing code to pull in the non-hidden order item metadata, and only show it for products in the "Events" category. Here's the modified function that does exactly what you need:
<?php /** * Display custom Event Registration details (item meta data from the order) * on the ORDER LIST/TABLE SCREEN via Admin Columns Pro * * Filter the display value for a column * * @param mixed $value Custom field value * @param int $id Object ID * @param AC_Column $column Column instance */ function fs_custom_event_registration_details_column_value( $value, $id, $column ) { if ( $column instanceof AC_Column_CustomField ) { $order = wc_get_order( $id ); $meta_key = $column->get_meta_key(); // Target our specific custom column if ( 'WooCommerceEventsTicketsPurchased' == $meta_key ) { // Get WooCommerce's default hidden order item meta keys (matches order edit page) $hidden_order_itemmeta = apply_filters( 'woocommerce_hidden_order_itemmeta', array( '_qty', '_tax_class', '_product_id', '_variation_id', '_line_subtotal', '_line_subtotal_tax', '_line_total', '_line_tax', 'method_id', 'cost', ) ); $event_meta_output = array(); foreach ( $order->get_items() as $item_id => $item ) { $product = wc_get_product( $item->get_product_id() ); // Only process products in the "Events" category if ( $product && has_term( 'Events', 'product_cat', $product ) ) { $product_name = $item->get_name(); $item_meta_list = array(); // Fetch formatted meta data just like the order edit page does if ( $meta_data = $item->get_formatted_meta_data( '' ) ) { foreach ( $meta_data as $meta ) { // Skip any hidden meta keys if ( ! in_array( $meta->key, $hidden_order_itemmeta, true ) ) { $item_meta_list[] = sprintf( '%s: %s', wp_kses_post( $meta->display_key ), wp_kses_post( force_balance_tags( $meta->display_value ) ) ); } } } // Build output for this event product if ( ! empty( $item_meta_list ) ) { $event_meta_output[] = sprintf( '<strong>%s</strong><ul><li>%s</li></ul>', $product_name, implode( '</li><li>', $item_meta_list ) ); } else { $event_meta_output[] = sprintf( '<strong>%s</strong><br>No additional details', $product_name ); } } } // Combine all event meta into the column value $value = ! empty( $event_meta_output ) ? implode( '<br><br>', $event_meta_output ) : 'No event tickets found'; } } return $value; } add_filter( 'ac/column/value', 'fs_custom_event_registration_details_column_value', 10, 3 );
Key Changes Explained:
- Consistent Hidden Meta Handling: We use WooCommerce's native
woocommerce_hidden_order_itemmetafilter to match exactly what gets hidden on the order edit page, so there's no inconsistency. - Category Targeting: We fetch the product object for each order item and use
has_term()to ensure we only process products in the "Events" category. - Clean Formatted Output: We pull the same formatted meta data that appears on the order edit page, filter out hidden keys, and display it as a bullet list under each product name for readability.
- Fallback Text: If there's no meta data for an event product, or no event products in the order at all, we show clear fallback text instead of empty content.
If you prefer comma-separated values instead of bullet lists, just swap out the formatting line:
// Replace bullet list formatting: $event_meta_output[] = sprintf( '<strong>%s</strong><ul><li>%s</li></ul>', $product_name, implode( '</li><li>', $item_meta_list ) ); // With comma-separated format: $event_meta_output[] = sprintf( '<strong>%s</strong>: %s', $product_name, implode( ', ', $item_meta_list ) );
内容的提问来源于stack exchange,提问作者Garconis
相关产品推荐
相关产品推荐

