【问题标题】:WooCommerce - add column to Product Cat admin tableWooCommerce - 将列添加到 Product Cat 管理表
【发布时间】:2014-12-20 06:49:53
【问题描述】:

我想在产品类别的后端表中添加一个新列。此列将包含一个链接“查看类别”,并将所有链接到 www.domain.com/category/category-name 页面。

我查看了 Wordpress 文档,这是我想出的代码……但它不起作用!

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');

知道我会怎么做吗?非常感谢您的帮助!

【问题讨论】:

    标签: php wordpress woocommerce taxonomy custom-taxonomy


    【解决方案1】:

    answer 中提取,您可以使用以下代码将列添加到编辑标签屏幕:

    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');
    

    【讨论】:

    • 感谢您发布此信息!我希望你不介意我发布了一个基于你的答案:)
    【解决方案2】:

    我发现要为这样一项微不足道的任务找到解决方案非常困难,我非常感谢 Helgatheviking 的回答为我指明了正确的方向。她的回答对我来说不太适用,因为它只允许所有列值使用相同的值,所以我决定在这里发布一个改进的版本。

    问题出在第二个函数上,因为它没有提供添加与当前类别对应的字段值的方法。我已经挖掘了Woocommerce's source(在那里你可以搜索“product_cat_column”来查看相关部分并查看它是如何制作的)并发现这个过滤器接受 3 个参数,而不是 1 个。这允许每行有一个特定的值,所有行的值都不相同,因为它在 Helgatheviking 的答案中。

    另一个缺点是它会将值放在缩略图的列中,因为这实际上是 Woocommerce 使用此过滤器的目的。

    这是我的代码:

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

    如您所见,第一个函数保持不变,但第二个函数现在检查列名并根据该名称返回内容。您可以通过这种方式获取任何类别元数据,并为任意数量的列执行此操作。

    【讨论】:

    • 我更新了代码,展示了如何将类别的链接添加到此列。
    • 不错的改进!你说得对,我的回答是……不完整。这是引用非常糟糕的 2014 年代码的一种慷慨方式,对吧?我赞成你的答案,它真的应该是“接受”的答案。
    猜你喜欢
    • 2016-07-26
    • 2020-06-18
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2021-04-15
    • 2018-09-05
    • 2018-09-09
    • 2019-07-18
    相关资源
    最近更新 更多