I want to get the formatted total amount of my cart items.

I used the following code

$order = commerce_cart_order_load($user->uid);
$wrapper = entity_metadata_wrapper('commerce_order', $order);
$line_items = $wrapper->commerce_line_items;
$total = commerce_line_items_total($line_items);
It returns me $4000 instead of $40.00.

I also tried the following code
$total = $wrapper->commerce_order_total->amount->value();
but same result.

On shoping cart page it display $40.00. How can I get the price in this format?

I want this in commerce way not some custom php like number_format etc.

Comments

mchampsee’s picture

The value returned is in pennies. Divide $total by 100 and you'll get the value in dollars.

bander2’s picture

Commerce provides a function for that:

http://www.drupalcontrib.org/api/drupal/contributions!commerce!commerce....

Commerce uses minor units (cents in USD) and converts only for display with the above function.

- Brendan

hmdnawaz’s picture

Yes used the following code and it is working.

$order = commerce_cart_order_load($user->uid);
$wrapper = entity_metadata_wrapper('commerce_order', $order);
$line_items = $wrapper->commerce_line_items;
$total = commerce_line_items_total($line_items);
$total_amount = commerce_currency_format($total['amount'], $total['currency_code']);

Ahmad Nawaz
Acquia Certified Developer
Email: hmdnawaz@gmail.com
Skype: hmdnawaz