Get woocommerce carts total amount

2019-01-18 09:59发布

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...

8条回答
叛逆
2楼-- · 2019-01-18 11:04

This works perfectly and removes currency symbol:

     $woocommerce->cart->total;
查看更多
叛逆
3楼-- · 2019-01-18 11:04

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.

查看更多
登录 后发表回答