WooCommerce - add column to Product Cat admin tabl

2019-07-20 12:02发布

I would like to add a new column in the back-end table for Product Categories. This column will contain a link "view category" and will link all to the www.domain.com/category/category-name page.

I looked into the Wordpress docs and this is the code I came up with... but it doesn't work!

function product_cat_cpt_columns($columns) {
    $new_columns = array(
        'Link' => "Link to page"
    );
    return array_merge($columns, $new_columns);
}
add_filter('manage_product_cat_posts_custom_columns' , 'product_cat_cpt_columns');

Any idea how I would do this? I really appreciate your help!

2条回答
疯言疯语
2楼-- · 2019-07-20 12:29

Pulling from this answer you can add columns to the Edit Tags screen with the following code:

function add_post_tag_columns($columns){
    $columns['foo'] = 'Foo';
    return $columns;
}
add_filter('manage_edit-product_cat_columns', 'add_post_tag_columns');

function add_post_tag_column_content($content){
    $content .= 'Bar';
    return $content;
}
add_filter('manage_product_cat_custom_column', 'add_post_tag_column_content');
查看更多
一夜七次
3楼-- · 2019-07-20 12:38

I found it suprisingly hard to find a solution for such a trivial task and I'm very thankful to Helgatheviking for her answer that pointed me to the right direction. Her answer didn't quite work for me, because it would allow only the same value for all the column values, so I decided to post an improved version here.

The problem was with the second function, because it didn't provide a way to add field's value that is corresponding to the current category. I've dug through Woocommerce's source (there you could search for "product_cat_column" to go through relevant parts and see how it's made) and found out that this filter accepts 3 paramaters, not 1. That allows for a specific value per row, not the same value for all rows, as it is in Helgatheviking's answer.

Another drawback was that it would put the value to the thumbnail's column, because that is what Woocommerce use this filter for, actually.

So here's my code:

function add_custom_column($columns) { 
    $columns['foo'] = 'FOO';
    $columns['link'] = 'Link to page';

    return $columns; 
} 
add_filter('manage_edit-product_cat_columns', 'add_custom_column'); 

function category_custom_column_value( $columns, $column, $term_id ) { 
    if ($column == 'FOO') {
        $foo = get_term_meta( $term_id, 'foo', true );
        return $foo;
    }elseif ($column == 'link') {
        $category = get_term_by( 'id', $term_id, 'product_cat' );
        $category_link = get_term_link( $category->slug, 'product_cat' );
        return '<a href="' . $category_link . '" target="_blank">' . $category_link . '</a>';
    }
}
add_filter('manage_product_cat_custom_column', 'category_custom_column_value', 10, 3);

As you can see, the first function stays the same, but the second now checks for the column name and returns the content depending on this name. You can get any category meta this way and do it for as many columns as you'd like.

查看更多
登录 后发表回答