普通网友 2014-05-25 17:50
浏览 67
已采纳

如何在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;
}

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

  • 写回答

4条回答 默认 最新

  • doufen3838 2014-05-29 15:30
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?