doushan1850 2018-10-06 23:05
浏览 205
已采纳

在Woocommerce 3中为管理产品列表添加自定义列产品可见性

I am trying to add a custom column to admin product list with the Catalog Visibility value of the products (basically, I need to know easier which is Hidden and which is not).

My code so far for my child theme's functions.php:

add_filter( 'manage_edit-product_columns', 'custom_product_column', 10);
function custom_product_column($columns){


 $columns['visibility'] = __( 'Visibility','woocommerce');
 return $columns;
}

    add_action( 'manage_product_posts_custom_column', 'custom_column_content', 10, 2 );

    function custom_product_list_column_content( $column, $product_id ){

    global $post;

$isitvisible = get_post_meta( $product_id, 'product_visibility', true );

switch ( $column ){

    case 'visibility' :
        echo $isitvisible;
        break;
  }
}

Can someone please guide me? The column is created (and the title displayed), but I get no data for the products.

  • 写回答

1条回答 默认 最新

  • dte8665 2018-10-07 00:09
    关注

    There are some errors and mistakes in your code. Also since Woocommerce 3 product visibility is handled by Woocommerce custom taxonomy 'product_visibility'. Try the following instead:

    // Add a new column to Admin products list with a custom order
    add_filter( 'manage_edit-product_columns', 'visibility_product_column', 10);
    function visibility_product_column($columns){
        $new_columns = [];
        foreach( $columns as $key => $column ){
            $new_columns[$key] = $columns[$key];
            if( $key == 'price' ) { // Or use: if( $key == 'featured' ) {
                 $new_columns['visibility'] = __( 'Visibility','woocommerce');
            }
        }
        return $new_columns;
    }
    
    // Add content to new column raows in Admin products list
    add_action( 'manage_product_posts_custom_column', 'visibility_product_column_content', 10, 2 );
    function visibility_product_column_content( $column, $product_id ){
        global $post;
    
        if( $column =='visibility' ){
            if( has_term( 'exclude-from-catalog', 'product_visibility', $product_id ) )
                echo '<em style="color:grey;">' . __("No") . '</em>';
            else
                echo '<span style="color:green;">' . __("Yes") . '</span>';
        }
    }
    

    Code goes in function.php file of your active child theme (active theme). Tested and works.

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?