在 Woocommerce 谢谢页面和电子邮件的订单总计中添加一个新行

Add a new row in order totals for Woocommerce Thankyou Page and emails

我对在电子邮件和感谢页面中添加一行的代码有疑问。

add_filter( 'woocommerce_get_order_item_totals', 'bbloomer_add_recurring_row_email', 10, 2 );
function bbloomer_add_recurring_row_email( $total_rows, $myorder_obj ) {
    $total_rows['recurr_not'] = array(
        'label' => __( 'Rec:', 'woocommerce' ),
        'value' => 'blabla'
    );
    return $total_rows;
}

我有一个功能可以在购物车中添加 "Total excl. VAT" 行:

<?php 
    global $woocommerce;
    $frais = 1.01; 
    echo '<tr class ="totalht">
    <th>'. __( 'Total HT', 'woocommerce' ) .'</th>
    <td data-title=" '. __( 'Total HT', 'woocommerce' ) .' ">'
    . wc_price( ( $woocommerce->cart->cart_contents_total * $frais ) + $woocommerce->cart->shipping_total ) .'<span class="ht-panier">HT</span></td>
    </tr>';
?>

效果很好:https://prnt.sc/irglky

但是当我修改第一个函数时:

add_filter( 'woocommerce_get_order_item_totals', 'bbloomer_add_recurring_row_email', 5, 2 );
function bbloomer_add_recurring_row_email( $total_rows, $myorder_obj ) {
    global $woocommerce;
    $frais = 1.01;
    $price_excl_vat = wc_price( ( $woocommerce->cart->cart_contents_total * $frais ) + $woocommerce->cart->shipping_total );
    $total_rows['recurr_not'] = array(
        'label' => __( 'Total HT :', 'woocommerce' ),
        'value' => $price_excl_vat 
    );

    return $total_rows;
}

没用on the Thank you page
但它正在工作 on email...

有人可以向我解释为什么它可以处理电子邮件但不能处理 "Thank you page"?

更新:由于此挂钩用于订单数据,但不是购物车数据,您应该试试这个,我设置了最后一行之前的附加行:

add_filter( 'woocommerce_get_order_item_totals', 'add_custom_order_totals_row', 30, 3 );
function add_custom_order_totals_row( $total_rows, $order, $tax_display ) {
    $costs = 1.01;

    // Set last total row in a variable and remove it.
    $gran_total = $total_rows['order_total'];
    unset( $total_rows['order_total'] );

    // Insert a new row
    $total_rows['recurr_not'] = array(
        'label' => __( 'Total HT :', 'woocommerce' ),
        'value' => wc_price( ( $order->get_total() - $order->get_total_tax() ) * $costs  ),
    );

    // Set back last total row
    $total_rows['order_total'] = $gran_total;

    return $total_rows;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。 已测试并有效


对于购物车,您应该使用此 (因为不再需要 global $woocommerce;:

<?php 
    $costs = 1.01; 

    echo '<tr class ="totalht">
    <th>'. __( 'Total HT', 'woocommerce' ) .'</th>
    <td data-title=" '. __( 'Total HT', 'woocommerce' ) .' ">'
    . wc_price( ( WC()->cart->cart_contents_total * $costs ) + WC()->cart->shipping_total ) .'<span class="ht-panier">HT</span></td>
    </tr>';
?>