Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Woocommerce review order total hook is echoing result twice

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?

like image 333
Raging Vids Avatar asked Nov 08 '25 09:11

Raging Vids


1 Answers

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.

like image 191
Ruvee Avatar answered Nov 10 '25 00:11

Ruvee