【问题标题】:WooCommerce: Add new column in products list view and make it sortable [duplicate]WooCommerce:在产品列表视图中添加新列并使其可排序[重复]
【发布时间】:2019-08-13 06:10:09
【问题描述】:

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

【问题讨论】:

标签: php wordpress sorting woocommerce product


【解决方案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 测试正常

【讨论】:

  • 这似乎有效。唯一的问题是如何增加列大小,现在它非常窄。还有一种方法可以使此列可排序吗?
  • 太棒了,谢谢!!!
【解决方案2】:

订单列表中的新列显示正确的重量,重量单位在 WooCommerce 设置中定义。只需将该代码放在主题文件夹中的 functions.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';
    }
}

尝试使用此代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 2021-06-26
    • 2021-05-26
    • 2019-03-12
    • 1970-01-01
    相关资源
    最近更新 更多