Woocommerce - 将订单重量添加到订单页面(HPOS 兼容)

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

我曾经使用一个插件将订单总重量添加到每个订单上,并显示在后端的订单页面上。 然而,HPOS打破了这一点,我一直在尝试编写代码来再次实现它。 我发现了一些弗兰肯斯坦的片段,因为我的 PHP 技能非常有限,据我所知,我似乎无法让它填充值。

我知道你们很多人看到这个都会对自己说,真是个白痴,但希望有人能够给我指出正确的方向

add_filter( 'manage_woocommerce_page_wc-orders_columns', 'add_order_weight_column' );
function add_order_weight_column( $columns ) {
       $columns['total_weight'] = __( 'Weight', 'woocommerce' );
    return $columns;
}

add_action( 'manage_shop_order_posts_custom_column', 'populate_order_weight_column_content', 10, 2 );
function populate_order_weight_column_content( $column ) {
     if ( $column == 'total_weight' ) {
        global $the_order;

        $total_weight = $the_order->get_meta('_cart_weight');

        if ( $total_weight > 0 ) {
            echo wc_format_weight( $total_weight );
        } else {
            _e('N/A', 'woocommerce');
        }
    }
}

该列不需要可排序,我只需要显示总重量

woocommerce hook-woocommerce
1个回答
0
投票

无论启用或不启用 HPOS,以下功能均适用:

add_filter( 'manage_woocommerce_page_wc-orders_columns', 'add_custom_columns_to_admin_orders', 20); // HPOS
add_filter( 'manage_edit-shop_order_columns', 'add_custom_columns_to_admin_orders', 20);
function add_custom_columns_to_admin_orders( $columns ) {
    return array_merge( $columns, ['total_weight' => __( 'Weight', 'woocommerce' )] );
}

add_action('manage_woocommerce_page_wc-orders_custom_column', 'custom_columns_content_in_admin_orders', 10, 2); // HPOS
add_action( 'manage_shop_order_posts_custom_column', 'custom_columns_content_in_admin_orders', 10, 2);
function custom_columns_content_in_admin_orders( $column, $order ) {
    if( ! is_a($order, 'WC_order') && $order > 0 ) {
        $order = wc_get_order( $order );
    }

    if ( 'total_weight' === $column ) {
        $total_weight = $order->get_meta('_cart_weight');
        echo $total_weight > 0 ? wc_format_weight($total_weight) : __('N/A', 'woocommerce');
    }
}

代码位于子主题的functions.php文件中(或插件中)。

© www.soinside.com 2019 - 2024. All rights reserved.