0

Trying to add the order subtotal in woocommerce my-account/orders table but I can't seem to get it to display. Currently it adds the column and the label but its not displaying the orders sub total. I am currently using the code below :

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 );
}
}
Walter
  • 323
  • 1
  • 4
  • 11
  • Just in case , I want to display sub total because total reflects amount minus the coupon (sometimes its 0) and i am building a custom project where i need to show the amount before coupon redemption – Walter Oct 02 '19 at 12:12

2 Answers2

1

Subtotal like in cart doesn't exist in WooCommerce Orders meta data, so you need to get it and calculate it from order items:

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;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Related:

helgatheviking
  • 25,596
  • 11
  • 95
  • 152
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • As always i'm pretty sure your code works Loic but on my webesite it added the column but not displaying the value, I've updated my question with the code that worked for me. Thanks for your reply and high involvement here on the stack wordpress community – Walter Oct 09 '19 at 09:00
0

Edit: the following code worked for me: (answer provided by helgatheviking on 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 );

}
}
Walter
  • 323
  • 1
  • 4
  • 11