问题描述
我正在尝试使用产品的目录可见性值将自定义列添加到管理产品列表(基本上,我需要更轻松地知道哪些是隐藏的,哪些不是).
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).
到目前为止我的子主题的functions.php代码:
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.
推荐答案
您的代码中存在一些错误和错误.此外,由于 Woocommerce 3 产品可见性由 Woocommerce 自定义分类法 'product_visibility'
处理.请尝试以下操作:
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>';
}
}
代码位于您的活动子主题(活动主题)的 function.php 文件中.经测试有效.
Code goes in function.php file of your active child theme (active theme). Tested and works.
这篇关于将自定义列产品可见性添加到 Woocommerce 3 中的管理产品列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!