简体   繁体   中英

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:

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' . 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.

在此输入图像描述

Woocommerce also allows you to hide products if they're out of stock. 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>';
        }
    }



 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM