Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

woocommerce - Get Cart Total as number

I want to get total price of cart in my woocommerce plugin.

I want to get it as a float number like so: 21.00 but I don't know how to get it. My code outputs weird results, this is my exact code:

$total         = $woocommerce->cart->get_total();
$total_a       = WC()->cart->get_total();
$total1        = $woocommerce->cart->get_total_ex_tax();
$total1_a      = WC()->cart->get_total_ex_tax();
$total2        = $woocommerce->cart->get_cart_total();
$total2_a      = WC()->cart->get_cart_total();

outputs:

0,00 €
0,00 €
0,00 €
0,00 €
21,00 €
21,00 €

and if I convert from string to float the result is of course 0.00.

Any help how to get cart total in the form of float number ?

like image 321
Marián Zeke Šedaj Avatar asked May 05 '15 21:05

Marián Zeke Šedaj


People also ask

How do I get the total order in WooCommerce?

Exactly, it is right there in the error message. If you have the order ID, you can $order = wc_get_order( $order_id ) to get the order object. Also $order->get_total() might mean you don't need to do all that preg_replace .


2 Answers

I have code like this and working perfect:

if ( ! WC()->cart->prices_include_tax ) {
    $amount = WC()->cart->cart_contents_total;
} else {
    $amount = WC()->cart->cart_contents_total + WC()->cart->tax_total;
}

good luck !

like image 51
huykon225 Avatar answered Oct 06 '22 00:10

huykon225


Just access the total property directly, it's public:

global $woocommerce;
echo $woocommerce->cart->total;
like image 25
José Avatar answered Oct 05 '22 23:10

José