如何在 Woocommerce 管理产品列表中添加/删除列
我想在查看产品列表时自定义 Woocommerce 管理区域中的列.
I want to customize the columns in Woocommerce admin area when viewing the product list.
具体来说,我想删除一些列,并添加几个自定义字段列.
Specifically, I want to remove some columns, and add several custom field columns.
我尝试了许多在线列出的解决方案,我可以删除列并添加新列,如下所示:
I tried many solutions listed online, and I can remove columns and add new ones like this:
add_filter( 'manage_edit-product_columns', 'show_product_order',15 );
function show_product_order($columns){
//remove column
unset( $columns['tags'] );
//add column
$columns['offercode'] = __( 'Offer Code');
return $columns;
}
但是我如何用实际的产品数据填充新列(在本例中是一个名为offercode"的自定义字段)?
But how do I populate the new column with the actual product data (in this case, a custom field called 'offercode')?
过滤器 manage_edit-{post_type}_columns
仅用于实际添加列.要控制每个帖子(产品)的列中显示的内容,您可以使用 manage_{post_type}_posts_custom_column
操作.为每个帖子的每个自定义列调用此操作,并传递两个参数:$column
和 $postid
.
The filter manage_edit-{post_type}_columns
is only used to actually add the column. To control what is displayed in the column for each post (product), you can use the manage_{post_type}_posts_custom_column
action. This action is called for each custom column for every post, and it passes two arguments: $column
and $postid
.
使用此操作非常简单,您可以在下面找到显示自定义字段offercode"的示例:
Using this action is quite easy, you can find an example to display the custom field "offercode" below:
add_action( 'manage_product_posts_custom_column', 'wpso23858236_product_column_offercode', 10, 2 );
function wpso23858236_product_column_offercode( $column, $postid ) {
if ( $column == 'offercode' ) {
echo get_post_meta( $postid, 'offercode', true );
}
}
您还可以使用插件来控制此行为,例如管理栏.
You could also use a plugin to control this behaviour, such as Admin Columns.