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

时间:2019-03-22 09:27:19

标签: php wordpress sorting woocommerce product

如何在管理产品列表表列表中再增加一列(权重)?

2 个答案:

答案 0 :(得分:1)

// 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_set_sortable_columns( $columns )
{
    $columns['total_weight'] = 'total_weight';
    return $columns;
}

add_filter( 'manage_edit-product_sortable_columns', 'my_set_sortable_columns' );

//排序功能

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进行了测试

答案 1 :(得分:0)

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

<?php
// Store cart weight in the database
add_action('woocommerce_checkout_update_order_meta', 'woo_add_cart_weight');
function woo_add_cart_weight( $order_id ) {
    global $woocommerce;
    
    $weight = $woocommerce->cart->cart_contents_weight;
    update_post_meta( $order_id, '_cart_weight', $weight );
}
// Add order new column in administration
add_filter( 'manage_edit-shop_order_columns', 'woo_order_weight_column', 20 );
function woo_order_weight_column( $columns ) {
	$offset = 8;
	$updated_columns = array_slice( $columns, 0, $offset, true) +
	array( 'total_weight' => esc_html__( 'Weight', 'woocommerce' ) ) +
	array_slice($columns, $offset, NULL, true);
	return $updated_columns;
}
// Populate weight column
add_action( 'manage_shop_order_posts_custom_column', 'woo_custom_order_weight_column', 2 );
function woo_custom_order_weight_column( $column ) {
	global $post;
 
	if ( $column == 'total_weight' ) {
		$weight = get_post_meta( $post->ID, '_cart_weight', true );
		if ( $weight > 0 )
			print $weight . ' ' . esc_attr( get_option('woocommerce_weight_unit' ) );
		else print 'N/A';
	}
}

尝试使用此代码。