Get woocommerce carts total amount

独自空忆成欢 提交于 2020-01-10 17:27:11

问题


I am trying to apply a discount to a carts total price, but I can only do it to the item base price and not the over all price. I Googled and came across this post in the wordpress stackoverflow:

$amount = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) ); The preg_replace eliminates everything but decimal characters and colons.

Should you care to do math with it, the floatval converts the value from a string to a numeric one.

I tried adding:

$amount2 = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );

and changing

$discount = round( (($discounting_amount / 100 ) *  $this->amount)*-1, WC()->cart->dp);

to

$discount = round( (($discounting_amount / 100 ) *  $amount2)*-1, WC()->cart->dp);

But I get the following error:

Fatal error: Call to a member function get_cart_total() on a non-object in...

回答1:


You need to call the global variable to ensure that it gets the correct values.

If you add

 global $woocommerce;

just before

 $amount2 = floatval( preg_replace( '#[^\d.]#', '', $woocommerce->cart->get_cart_total() ) );

that should solve your problem.




回答2:


Try this:

WC()->cart->cart_contents_total

The function get_cart_total uses wc_price function thas converts cart_contents_total to currency.




回答3:


This also work nicelly.

WC()->cart->total




回答4:


This works perfectly and removes currency symbol:

     $woocommerce->cart->total;



回答5:


global $woocommerce;
    $amount = $woocommerce->cart->cart_contents_total+$woocommerce->cart->tax_total;

You can also convert $amount in float value as per your requirement.




回答6:


As of late 2018, the best way is to use get_cart_contents_total(). This is the total of items in the cart after discounts.

WC()->cart->get_cart_contents_total(); // Float

Other methods are available for more specific needs, just have a look at the docs.




回答7:


To show the carts total including tax and discounts use this

$ordertotal = wp_kses_data( WC()->cart->get_total() );



回答8:


$totalamount = $woocommerce->cart->cart_contents_total;

echo $totalamount;



来源:https://stackoverflow.com/questions/22249615/get-woocommerce-carts-total-amount

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!