How to add / remove columns in Woocommerce admin product list
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!
Here is a list of some $columns used by Woocommerce:
$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;
}
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
.
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.
In case someone wants to insert in a certain order, here is how to add the column to be right after 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 ;
}
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):
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;
}