新增SKU数据列@WordPress独立站后台的订单列表页面
先看效果图:
代码如下:将下面代码添加到后台的外观-主题文件编辑器functions.php文件:
// 添加SKU列到订单列表页 function custom_wc_order_sku_column( $columns ) { $new_columns = array(); foreach ( $columns as $key => $value ) { $new_columns[ $key ] = $value; if ( 'order_status' === $key ) { $new_columns['order_sku'] = 'SKU'; } } return $new_columns; } add_filter( 'manage_edit-shop_order_columns', 'custom_wc_order_sku_column' ); // 填充SKU列内容 function custom_wc_order_sku_column_content( $column, $post_id ) { if ( 'order_sku' === $column ) { $order = wc_get_order( $post_id ); $items = $order->get_items(); $skus = array(); foreach ( $items as $item ) { $product = $item->get_product(); if ( is_a( $product, 'WC_Product' ) ) { $sku = $product->get_sku(); if ( ! empty( $sku ) ) { $skus[] = $sku; } } } if ( ! empty( $skus ) ) { echo implode( ', ', $skus ); } } } add_action( 'manage_shop_order_posts_custom_column', 'custom_wc_order_sku_column_content', 10, 2 );