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

问题描述 投票:0回答:3

我正在尝试使用产品的目录可见性值向管理产品列表添加自定义列(基本上,我需要更容易地知道哪些是隐藏的,哪些不是)。

到目前为止我的子主题的function.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;
  }
}

有人可以指导我吗?该列已创建(并显示标题),但我没有获得产品的数据。

php wordpress woocommerce product visibility
3个回答
6
投票

您的代码中存在一些错误和错误。此外,由于 Woocommerce 3 产品可见性由 Woocommerce 自定义分类法

'product_visibility'
处理。请尝试以下方法:

// 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 文件中。已测试并有效。


0
投票

Woocommerce 还允许您隐藏缺货的产品。我需要知道哪些产品被排除在目录之外,哪些产品因为缺货而被隐藏。对上面代码的这个小更新使用一个数组来查找我需要知道的所有隐藏条件:

// 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>';
        }
    }



 

0
投票

// 将内容添加到管理产品列表中的新列行 add_action( 'manage_product_posts_custom_column', 'visibility_product_column_content', 10, 2 ); 函数visibility_product_column_content($column,$product_id){ 全球$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>';
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.