Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

php - How to add / remove columns in Woocommerce admin product list

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...