我想在 WooCommerce 管理员订单列表中添加带有一些元数据的自定义列。我发现并根据需要修改了它,并将其放入我的 functions.php:
add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION' );
function MY_COLUMNS_FUNCTION( $columns ) {
$new_columns = ( is_array( $columns ) ) ? $columns : array();
unset( $new_columns[ 'order_actions' ] );
//edit this for your column(s)
//all of your columns will be added before the actions column
$new_columns['dname'] = 'Dogs Name';
$new_columns['additional_allergies'] = 'Allergies';
//stop editing
$new_columns[ 'order_actions' ] = $columns[ 'order_actions' ];
return $new_columns;
}
结帐时我收集了这两个元密钥:
但是,我当前的代码只显示空列,添加元数据有什么建议吗?
manage_shop_order_posts_custom_column
钩子可用于添加标题。
// Add a Header
function custom_shop_order_column( $columns ) {
// Add new columns
$columns['dogs_name'] = __( 'Dogs Name', 'woocommerce' );
$columns['additional_allergies'] = __( 'Allergies', 'woocommerce' );
return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 10, 1 );
manage_shop_order_posts_custom_column
钩子将需要填充列。
$order
对象的元数据还是属于订单 $items
的元数据,并且基于此您将必须使用以下 2 个答案中的 1 个.1a: 如果元数据属于
$order
对象,您可以使用:
// Populate the Column
function custom_shop_order_list_column_content( $column, $post_id ) {
// Get order object
$order = wc_get_order( $post_id );
// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
// Compare column name
if ( $column == 'dogs_name' ) {
// Get meta, use the correct meta key!
$dogs_name = $order->get_meta( 'dname' );
// NOT empty
if ( ! empty( $dogs_name ) ) {
// Output
echo $dogs_name;
} else {
// Output
echo __( 'Meta key is wrong or not found for this order', 'woocommerce' );
}
}
// Compare column name
if ( $column == 'additional_allergies' ) {
// Get meta, use the correct meta key!
$allergies = $order->get_meta( 'additional_allergies' );
// NOT empty
if ( ! empty( $allergies ) ) {
// Output
echo $allergies;
} else {
// Output
echo __( 'Meta key is wrong or not found for this order', 'woocommerce' );
}
}
}
}
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_list_column_content', 10, 2 );
1b: 然而,当元数据属于顺序
$items
时,上面的答案将需要进行调整,因为一个$order
可以由多个$items
组成,我们将循环遍历$order
对象与foreach
// Populate the Column
function custom_shop_order_list_column_content( $column, $post_id ) {
// Get order object
$order = wc_get_order( $post_id );
// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
// Initialize
$dogs_name_arr = array();
$allergies_arr = array();
// Loop trough order items
foreach ( $order->get_items() as $item_key => $item ) {
// Get meta, use the correct meta key!
$dogs_name = $item->get_meta( 'dname' );
// NOT empty
if ( ! empty ( $dogs_name ) ) {
// Push to array
$dogs_name_arr[] = $dogs_name;
}
// Get meta, use the correct meta key!
$allergies = $item->get_meta( 'additional_allergies' );
// NOT empty
if ( ! empty ( $allergies ) ) {
// Push to array
$allergies_arr[] = $allergies;
}
}
// Compare column name
if ( $column == 'dogs_name' ) {
// NOT empty
if ( ! empty ( $dogs_name_arr ) ) {
// Output
echo '<ul>';
echo '<li>' . implode( '</li><li>', $dogs_name_arr ) . '</li>';
echo '</ul>';
} else {
// Output
echo __( 'Meta key is wrong or not found for the order items', 'woocommerce' );
}
}
// Compare column name
if ( $column == 'additional_allergies' ) {
// NOT empty
if ( ! empty ( $allergies_arr ) ) {
// Output
echo '<ul>';
echo '<li>' . implode( '</li><li>', $allergies_arr ) . '</li>';
echo '</ul>';
} else {
// Output
echo __( 'Meta key is wrong or not found for the order items', 'woocommerce' );
}
}
}
}
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_list_column_content', 10, 2 );