php 如何在 Woocommerce 管理产品列表中添加/删除列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23858236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:00:52  来源:igfitidea点击:

How to add / remove columns in Woocommerce admin product list

phpwordpresswoocommerceproductbackend

提问by lilbiscuit

I want to customize the columns in Woocommerce admin area when viewing the product list.

我想在查看产品列表时自定义 Woocommerce 管理区域中的列。

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;
}

But how do I populate the new column with the actual product data (in this case, a custom field called 'offercode')?

但是我如何用实际的产品数据填充新列(在这种情况下,一个名为“offercode”的自定义字段)?

回答by engelen

The filter manage_edit-{post_type}_columnsis 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_columnaction. This action is called for each custom column for every post, and it passes two arguments: $columnand $postid.

过滤器manage_edit-{post_type}_columns仅用于实际添加列。要控制每个帖子(产品)的列中显示的内容,您可以使用该manage_{post_type}_posts_custom_column操作。为每个帖子的每个自定义列调用此操作,并传递两个参数:$column$postid

Using this action is quite easy, you can find an example to display the custom field "offercode" below:

使用此操作非常简单,您可以在下面找到显示自定义字段“offercode”的示例:

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.

您还可以使用插件来控制此行为,例如Admin Columns

回答by guido _nhcol.com.br_

this table view is used by many plugins and wordpress itself. You have to check the column name. $columns['tags'] is the tag in Wordpress Post View, not in Woocommerce!

这个表格视图被许多插件和 wordpress 本身使用。您必须检查列名。$columns['tags'] 是 Wordpress Post View 中的标签,而不是 Woocommerce!

Here is a list of some $columns used by Woocommerce:

以下是 Woocommerce 使用的一些 $columns 的列表:

$columns['cb']  
$columns['thumb']
$columns['name'] 
$columns['sku'] 
$columns['is_in_stock']
$columns['price']
$columns['product_cat'] 
$columns['product_tag']
$columns['featured']
$columns['product_type']
$columns['date']

and that is the correct filter to apply these removals.

这是应用这些删除的正确过滤器。

add_filter( 'manage_edit-product_columns', 'change_columns_filter',10, 1 );
function change_columns_filter( $columns ) {
unset($columns['product_tag']);
unset($columns['sku']);
unset($columns['featured']);
unset($columns['product_type']);
return $columns;
}

回答by Marcel Lange

If you additionally want to sort the columns (as shown above, your column will just be attached to the end), you can do something like that in your hook of "manage_edit-product_columns" (example is taken from a class I've implemented):

如果您还想对列进行排序(如上所示,您的列将仅附加到末尾),您可以在“manage_edit-product_columns”的钩子中执行类似的操作(示例取自我实现的类):

const BACKEND_PRODUCT_GRID_FIELD_SORTORDER = [
    'cb',
    'thumb',
    'name',
    'pa_size_text',
    'sku',
    'is_in_stock',
    'price',
    'product_cat',
    'product_tag',
    'featured',
    'product_type',
    'date',
    'stats',
    'likes'
];

/**
 * Registers new columns for the backend products grid of Woocommerce.
 * Additionally it sorts the fields after
 * self::BACKEND_PRODUCT_GRID_FIELD_SORTORDER. Fields not included in
 * self::BACKEND_PRODUCT_GRID_FIELD_SORTORDER will be attached to the end of
 * the array.
 *
 * @param array $aColumns - the current Woocommerce backend grid columns
 *
 * @return array - the extended backend grid columns array
 */
public function add_columns_to_product_grid( $aColumns ) {
    $aColumns['pa_size_text'] = __( 'Unit size', 'intolife_misc' );
    #unset($aColumns['thumb']);
    $aReturn = [];
    foreach ( self::BACKEND_PRODUCT_GRID_FIELD_SORTORDER as $sKey ) {
        if ( isset( $aColumns[ $sKey ] ) ) {
            $aReturn[ $sKey ] = $aColumns[ $sKey ];
        }
    }

    /**
     * search additional unknown fields and attache them to the end
     */
    foreach ( $aColumns as $sKey => $sField ) {
        if ( ! isset( $aReturn[ $sKey ] ) ) {
            $aReturn[ $sKey ] = $sField;
        }
    }

    return $aReturn;
}

回答by harrrrrrry

In case someone wants to insert in a certain order, here is how to add the column to be right after Price:

如果有人想按特定顺序插入,这里是如何将列添加到紧随其后Price

add_filter( 'manage_edit-product_columns', 'wootix_show_product_order', 15 ) ;

function wootix_show_product_order( $columns )
{
    //add column
    $arr = array( 'wootix_credit' => __( 'Credits', 'wootix' ) ) ;

    array_splice( $columns, 6, 0, $arr ) ;

    return $columns ;
}