重新排列 WooCommerce 电子邮件通知中的订单详细信息总数

Rearrange order detail totals on WooCommerce email notifications

我正在 WooCommerce 中自定义订单电子邮件模板,需要在订单详细信息中将 "Shipping" 设为倒数第二个,就在 "total" 上方。

我知道这个循环在 woocommerce>templates>emails 的 "email-order-details.php" 页面的第 52 行,所以在我的子主题中设置它,但我不确定从那里去哪里。这是我正在尝试的:

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

使用挂钩在 woocommerce_get_order_item_totals 过滤器挂钩中的自定义函数,将允许按预期重新排序项目总数:

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

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

已测试并有效。