简体   繁体   English

将自定义列产品可见性添加到 Woocommerce 3 中的管理产品列表

[英]Add custom column product visibility to admin product list in 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:到目前为止,我的子主题的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.该列已创建(并显示标题),但我没有获得产品的数据。

There are some errors and mistakes in your code. 您的代码中存在一些错误和错误。 Also since Woocommerce 3 product visibility is handled by Woocommerce custom taxonomy 'product_visibility' . 此外,Woocommerce 3产品可视性由Woocommerce自定义分类'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). 代码位于活动子主题(活动主题)的function.php文件中。 Tested and works. 经过测试和工作。

在此输入图像描述

Woocommerce also allows you to hide products if they're out of stock. Woocommerce 还允许您隐藏缺货的产品。 I needed to know which were excluded from catalog and which were hidden because they were out of stock.我需要知道哪些被排除在目录之外,哪些因为缺货而被隐藏。 This small update to the code above uses an array to find all the hidden conditions I needed to know:上面代码的这个小更新使用一个数组来查找我需要知道的所有隐藏条件:

// Add content to new column rows 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( array('exclude-from-catalog', 'outofstock'),'product_visibility', $product_id ) )
                echo '<em style="color:grey;">' . __("No") . '</em>';
                   else
              echo '<span style="color:green;">' . __("Yes") . '</span>';
        }
    }



 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce - 如何在管理订单页面中向产品列表添加列 - WooCommerce - How to add column to product list in admin order page WooCommerce - 将列添加到Product Cat管理表 - WooCommerce - add column to Product Cat admin table WooCommerce:在管理产品列表中移动列位置 - WooCommerce: Moving column position in admin product list 在 WooCommerce 中向管理产品批量编辑表单添加产品自定义字段 - Add a product custom field to Admin product bulk edit form in WooCommerce 在woocommerce管理产品列表中为产品标签添加过滤器下拉列表 - Add a filter dropdown for product tags in woocommerce admin product list 将自定义元列添加到 WooCommerce 产品列表 - Add custom meta column to WooCommerce product listing 如何将新列添加到管理产品列表 - how to add new column to admin product list WooCommerce:如何在管理产品列表中的产品名称旁边添加产品类型 - WooCommerce: How to add product type next to product name in admin product list 在 Woocommerce Admin 产品列表中的产品类别过滤器之后添加自定义分类过滤器 - Add a custom taxonomy filter after product category filter in Woocommerce Admin products list 如何在管理员的 Woocommerce 产品中添加自定义字段以上传 PDF 文件 - How to add custom field for upload PDF files in Woocommerce product in admin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM