Williams
Williams

Reputation: 451

Customize total rows from WooCommerce order table

In WooCommerce, I understand well that woocommerce_get_order_item_totals filter kook is used to customize order total rows like reordering them.

add_filter( 'woocommerce_get_order_item_totals', 'custom_order_of_from_order_table', 10, 2 );
function woocommerce_get_order_item_totals( $total_rows, $order ) {
    // code here
    
    return $total_rows;
}

I have tried to reorder the subtotal over the total, and the payment method below the total without success on WooCommerce ThankYou page. My PHP knowledge is very limited and I appreciate any help.

How to customize total rows from order table, reordering them on WooCommerce thankyou page?

Upvotes: 1

Views: 1212

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

The following will reorder items totals as desired on Woocommerce thankyou (order received) page only:

add_filter( 'woocommerce_get_order_item_totals', 'reordering_order_item_totals', 10, 3 );
function reordering_order_item_totals( $total_rows, $order, $tax_display = '' ){
    // Only on "order received" thankyou page
    if ( ! is_wc_endpoint_url('order-received') )
        return $total_rows;

    $sorted_items_end  = array('cart_subtotal', 'order_total', 'payment_method');
    $sorted_total_rows = array(); // Initializing

    // Loop through sorted totals item keys
    foreach( $sorted_items_end as $item_key ) {
        if( isset($total_rows[$item_key]) ) {
            $sorted_total_rows[$item_key] = $total_rows[$item_key]; // Save sorted data in a new array
            unset($total_rows[$item_key]); // Remove sorted data from default array
        }
    }

    return array_merge( $total_rows, $sorted_total_rows); // merge arrays
}

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


To make that work everywhere for customer orders and email notifications, just remove:

// Only on "order received" thankyou page
if ( ! is_wc_endpoint_url('order-received') )
    return $total_rows;

Upvotes: 2

Related Questions