【问题标题】:Add custom column product visibility to admin product list in Woocommerce 3将自定义列产品可见性添加到 Woocommerce 3 中的管理产品列表
【发布时间】:2019-03-12 01:13:00
【问题描述】:

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

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

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

【问题讨论】:

    标签: php wordpress woocommerce product visibility


    【解决方案1】:

    您的代码中有一些错误和错误。此外,由于 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 文件中。经过测试并且可以工作。

    【讨论】:

    • 谢谢你,@LoicTheAztec。它有效,但是,我有一些问题:1)这个词不应该是'hidden'而不是'exclude-from-catalog'吗? 2) 如何更改可见性列的位置?我希望它位于“精选”列之前或之后。将add_filter() 的优先级值更改为 1,只会将该列向左移动一点。
    • 1) 不,它是“从目录中排除”... 2) 已更新...不要更改挂钩优先级。
    • 在前端隐藏/不可见的产品会发生什么?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2018-01-23
      • 2020-06-18
      • 1970-01-01
      • 2019-06-28
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 2018-11-11
      • 2019-01-13
      相关资源
      最近更新 更多