为Woocommerce Thankyou Page和电子邮件添加订单总计的新行

时间:2018-03-15 09:19:30

标签: php wordpress woocommerce hook-woocommerce orders

我在使用此代码时遇到问题,需要在电子邮件中添加一行&谢谢你的页面。

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

有人可以解释我为什么要处理电子邮件,而不是在&#34;感谢您的页面&#34;?

1 个答案:

答案 0 :(得分:1)

已更新:由于此挂钩适用于订单数据,但不是购物车数据,您应该尝试这样做,我在最后一行之前设置附加行:< / p>

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文件中。 经过测试并正常工作

enter image description here

对于购物车,您应该使用此(因为不再需要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>';
?>