Php WooCommerce:在产品列表视图中添加新列并使其可排序

Php WooCommerce:在产品列表视图中添加新列并使其可排序,php,wordpress,sorting,woocommerce,product,Php,Wordpress,Sorting,Woocommerce,Product,如何在“管理产品”列表中再添加一列(重量)?订单列表中的一个新列显示正确的重量,重量单位在WooCommerce设置中定义。只需将代码放入主题文件夹中的functions.php: //在数据库中存储购物车重量 添加操作(“woocommerce\u checkout\u update\u order\u meta”、“woo\u add\u cart\u weight”); 功能woo\ U添加\购物车\重量($order\ U id){ 全球商业; $weight=$woocommerce-

如何在“管理产品”列表中再添加一列(重量)?

订单列表中的一个新列显示正确的重量,重量单位在WooCommerce设置中定义。只需将代码放入主题文件夹中的functions.php

//在数据库中存储购物车重量
添加操作(“woocommerce\u checkout\u update\u order\u meta”、“woo\u add\u cart\u weight”);
功能woo\ U添加\购物车\重量($order\ U id){
全球商业;
$weight=$woocommerce->cart->cart\u contents\u weight;
更新发布元($order\U id、$cart\U weight、$weight);
}
//在管理中添加订单新列
添加过滤器(“管理编辑车间订单列”、“采购订单重量列”,20);
函数woo_顺序_重量_列($columns){
$offset=8;
$updated_columns=数组_切片($columns,0,$offset,true)+
数组('total_weight'=>esc_html_uuu('weight','woocommerce'))+
数组_切片($columns,$offset,NULL,true);
返回$U列;
}
//填充权重列
添加操作('manage_shop_order_posts_custom_column','woo_custom_order_weight_column',2);
函数woo\自定义\订单\重量\列($column){
全球$员额;
如果($column==‘总重量’){
$weight=get_post_meta($post->ID,'u cart_weight',true);
如果($weight>0)
打印$weight.'''.esc_attr(获取选项('woocommerce_weight_unit');
否则打印“不适用”;
}
}
尝试使用此代码

// Add product new column in administration
add_filter( 'manage_edit-product_columns', 'woo_product_weight_column', 20 );
function woo_product_weight_column( $columns ) {

    $columns['total_weight'] = esc_html__( 'Weight', 'woocommerce' );
        return $columns;

}
// Populate weight column
add_action( 'manage_product_posts_custom_column', 'woo_product_weight_column_data', 2 );
function woo_product_weight_column_data( $column ) {
    global $post;

    if ( $column == 'total_weight' ) {
        $product = wc_get_product($post->ID);
                $weight = $product->get_weight();
        if ( $weight > 0 )
            print $weight . ' ' . esc_attr( get_option('woocommerce_weight_unit' ) );
        else print 'N/A';
    }
}
//为列宽添加CSS

add_action('admin_head', 'my_column_width');

function my_column_width() {
    echo '<style type="text/css">';
    echo 'table.wp-list-table .column-total_weight { width: 46px; text-align: left!important;padding: 5px;}';
    echo '</style>';
}
//排序功能

function my_sort_custom_column_query( $query )
    {
        $orderby = $query->get( 'orderby' );

        if ( 'total_weight' == $orderby ) {

            $meta_query = array(
                'relation' => 'OR',
                array(
                    'key' => '_weight',
                    'compare' => '>', // see note above
                ),
                array(
                    'key' => '_weight',
                ),
            );

            $query->set( 'meta_query', $meta_query );
            $query->set( 'orderby', 'meta_value' );
        }
    }

    add_action( 'pre_get_posts', 'my_sort_custom_column_query' );

使用WooCommerce 3.5.7测试正常

我想您可以在这里找到解决方案,希望这对您有所帮助。@MarshallDudeja感谢您的参考。我要编辑哪个文件?php?是的,您需要在functions.php中添加新代码。这似乎很有效。唯一的问题是如何增加列的大小,现在它非常窄。还有什么方法可以让这个专栏分类吗?太好了,谢谢!!!
function my_sort_custom_column_query( $query )
    {
        $orderby = $query->get( 'orderby' );

        if ( 'total_weight' == $orderby ) {

            $meta_query = array(
                'relation' => 'OR',
                array(
                    'key' => '_weight',
                    'compare' => '>', // see note above
                ),
                array(
                    'key' => '_weight',
                ),
            );

            $query->set( 'meta_query', $meta_query );
            $query->set( 'orderby', 'meta_value' );
        }
    }

    add_action( 'pre_get_posts', 'my_sort_custom_column_query' );