如何在Woocommerce管理产品列表中添加/删除列 [英] How to add / remove columns in Woocommerce admin product list

查看:208
本文介绍了如何在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.

这篇关于如何在Woocommerce管理产品列表中添加/删除列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆