问题
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