添加 "sub-total" 到 my-account/orders table

Add "sub-total" to my-account/orders table

尝试在 woocommerce my-account/orders table 中添加订单小计,但我似乎无法显示它。目前它添加了列和标签,但不显示订单小计。我目前正在使用以下代码:

add_filter( 'woocommerce_account_orders_columns', 
'add_account_orders_column', 10, 1 );
function add_account_orders_column( $columns ){
$columns['item_subtotal_tax_excl'] = __( 'Sub-total', 'woocommerce' );

return $columns;
}

add_action( 'woocommerce_my_account_my_orders_column_custom-column', 
'add_account_orders_column_rows' );
function add_account_orders_column_rows( $order ) {
// Example with a custom field
if ( $value = $order->get_meta( 'item_subtotal_tax_excl' ) ) {
    echo esc_html( $value );
}
}

WooCommerce Orders 元数据中不存在购物车中的小计,因此您需要获取它并从订单项目中计算它:

add_filter( 'woocommerce_account_orders_columns', 'add_account_orders_column', 10, 1 );
function add_account_orders_column( $columns ){
    $columns['item_subtotal_tax_excl'] = __( 'Sub-total', 'woocommerce' );

    return $columns;
}

add_action( 'woocommerce_my_account_my_orders_column_custom-column', 'display_account_orders_column_rows_value' );
function display_account_orders_column_rows_value( $order ) {
    $subtotal = 0; // Initializing
    
    // Loop through order items (line items)
    foreach ( $order->get_items() as $item ) {
        // Sum item subtotal excluding taxes and not discounted 
        $subtotal += $item->get_subtotal(); 
    }
    
    echo $subtotal;
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。

相关:

编辑:以下代码对我有用:(helgatheviking 在 woocommerce slack 上提供的答案)

//show sub total in order page

add_filter( 'woocommerce_account_orders_columns', 
'add_account_orders_column', 10, 1 );

function add_account_orders_column( $columns ){

$columns['item_subtotal_tax_excl'] = __( 'Sub-Total', 
'woocommerce' );

return $columns;

}

add_action( 
'woocommerce_my_account_my_orders_column_item_subtotal_tax_excl', 
'add_account_orders_column_rows' );

function add_account_orders_column_rows( $order ) {

// Example with a custom field

if ( $value = $order->get_subtotal() ) {

echo esc_html( $value );

}
}