Walter
Walter

Reputation: 363

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

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

Upvotes: 0

Views: 334

Answers (2)

LoicTheAztec
LoicTheAztec

Reputation: 254182

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:

Upvotes: 1

Walter
Walter

Reputation: 363

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

}
}

Upvotes: 0

Related Questions