Home > database >  Woocommerce review order total hook is echoing result twice
Woocommerce review order total hook is echoing result twice

Time:10-05

I want to get the cart total figure for which I am using the following code in my functions.php:

function display_total(){
    global $woocommerce;
    $newTotal = $woocommerce->cart->get_total();

    echo $newTotal;
};
add_action( 'woocommerce_review_order_before_order_total', 'display_total');

Instead of displaying the amount once, it does it twice like $18.00$18.00.

Doing a var_dump too results in 2 lines of HTML:

D:\Wordpress\wp-content\themes\new_theme\functions.php:161:string '<span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">&#36;</span>18.00</bdi></span>' (length=128)

D:\Wordpress\wp-content\themes\new_theme\functions.php:161:string '<span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">&#36;</span>18.00</bdi></span>' (length=128)

What am I doing wrong?

CodePudding user response:

Echo the value in a table row, so that it won't echo it twice. So your code would be something like this:

add_action( 'woocommerce_review_order_before_order_total', 'display_total');

function display_total(){
    global $woocommerce;
    $newTotal = $woocommerce->cart->get_total();

    echo '<tr><td >' . $newTotal . '</td></tr>';
};

And the reason is, woocommerce_review_order_after_order_total hook is being used in a table, therefore adding your value between a tr and td tag would result in echoing the value once.

  • Related