最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Woocommerce get cart total price in a number format

programmeradmin3浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic.

Closed 11 years ago.

Improve this question

Is it possible to get the cart total price without any markup. So without the € symbol? Right now I'm getting the amount with:

$totalamount = $woocommerce->cart->get_cart_total();  

this will give €16.50

I tried this also:

$totalamount = number_format($woocommerce->cart->get_cart_total(), 2, '.', '');

But this always gives 0.00

Is there a woocommerce get function that will give a number format of the total cart price? Thanks!

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic.

Closed 11 years ago.

Improve this question

Is it possible to get the cart total price without any markup. So without the € symbol? Right now I'm getting the amount with:

$totalamount = $woocommerce->cart->get_cart_total();  

this will give €16.50

I tried this also:

$totalamount = number_format($woocommerce->cart->get_cart_total(), 2, '.', '');

But this always gives 0.00

Is there a woocommerce get function that will give a number format of the total cart price? Thanks!

Share Improve this question asked Sep 24, 2013 at 11:39 TrekdropTrekdrop 2451 gold badge4 silver badges12 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 9

Update 2020

Answer

See flytech's answer for a solution using the native WooCommerce API.

Note / Caveat

If you're going to do proper arithmetic with monetary values, always use signed integers (!) representing the smallest denomination of a given currency (Cent, Penny, Paisa, Dirham, e.g.).
Only convert back to decimal fractions in the presentation layer of your application after all calculations are done.

This holds true regardless of language or framework.

Original answer

I don't know woocommerce at all and hence there might be a native way as well, but anyhow, this

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

should do.

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.

That is what you want:

Working with global variable:

global $woocommerce;  
$woocommerce->cart->total;

Working with function:

WC()->cart->total;
发布评论

评论列表(0)

  1. 暂无评论