Arthur M
Arthur M

Reputation: 53

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

I have a problem with this code for add a row in email & thank you page.

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

I have a function to add "Total excl. VAT" row in the cart :

<?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>';
?>

It's working good : https://prnt.sc/irglky

But when I modify the first function :

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

It's not working on the Thank you page
But it's working on email...

Someone can explain me why it's working on email but not in the "Thank you page"?

Upvotes: 3

Views: 2901

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

Updated: As this hook is for Order data, but Not cart data, you should try this instead, where I set the additional row before the last one:

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

Code goes in function.php file of your active child theme (or active theme). Tested and works.

enter image description here


For cart you should use this (as global $woocommerce; is no longer needed):

<?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>';
?>

Upvotes: 3

Related Questions