Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rearrange order detail totals on WooCommerce email notifications

I am customizing the order email template in WooCommerce and need to make "Shipping" second-last in the order details, right above "total".

enter image description here

I know the loop for this is on line 52 in the "email-order-details.php" page from woocommerce>templates>emails, so set it up in my child theme but I'm not sure where to go from there. Here is what I'm trying:

if ( $totals = $order->get_order_item_totals() ) {
                $i = 0;
                foreach ( $totals as $total ) {
                    $i++;
                    if($total['label'] === "Shipping"){
                        //make second-last above total somehow
                    }
                    else{
                        ?><tr>
                        <th class="td" scope="row" colspan="3" style="text-align:<?php echo $text_align; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>"><?php echo $total['label']; ?></th>
                        <td class="td" style="text-align:left; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : ''; ?>" colspan="1"><?php echo $total['value']; ?></td>
                        </tr><?php
                    }
                }
            }
like image 242
Seb G Avatar asked Dec 22 '17 14:12

Seb G


1 Answers

Using a custom function hooked in woocommerce_get_order_item_totals filter hook, will allow to reorder item totals as expected:

add_filter( 'woocommerce_get_order_item_totals', 'reordering_order_item_totals', 10, 3 );
function reordering_order_item_totals( $total_rows, $order, $tax_display ){
    // 1. saving the values of items totals to be reordered
    $shipping = $total_rows['shipping'];
    $order_total = $total_rows['order_total'];

    // 2. remove items totals to be reordered
    unset($total_rows['shipping']);
    unset($total_rows['order_total']);

    // 3 Reinsert removed items totals in the right order
    $total_rows['shipping'] = $shipping;
    $total_rows['order_total'] = $order_total;

    return $total_rows;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works.

enter image description here

like image 137
LoicTheAztec Avatar answered Oct 29 '22 15:10

LoicTheAztec