Add custom column product visibility to admin product list in Woocommerce 3

℡╲_俬逩灬. 提交于 2021-01-21 10:33:30

问题


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:


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.



来源:https://stackoverflow.com/questions/52684073/add-custom-column-product-visibility-to-admin-product-list-in-woocommerce-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!