Rearrange order detail totals on WooCommerce email notifications
Asked Answered
D

1

5

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
                    }
                }
            }
Demoniac answered 22/12, 2017 at 14:18 Comment(0)
T
10

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

Triparted answered 22/12, 2017 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.