0

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.

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.

3 Answers 3

1

I had the same issue. I solved this issue by dividing the total by 100. You can see the total amount in the field_revision_commerce_total table. After dividing, I used commerce_currency_format($amount,NULL,NULL) to get the currency format.

5
  • 2
    Does not need to divide by 100. Used the following code and its done. $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
    Commented Oct 15, 2015 at 10:24
  • Yes it's working.
    – user49951
    Commented Oct 15, 2015 at 12:22
  • Ahmad, could you post your result as an answer so it's formatted better? will help others
    – troseman
    Commented Oct 15, 2015 at 20:17
  • $mysql=db_query("SELECT commerce_total_amount ,commerce_total_currency_code FROM field_revision_commerce_total WHERE entity_id =$order_id"); $data1 = $mysql->fetchObject(); $total_amount=$data1->commerce_total_amount; $currency_code=$data1->commerce_total_currency_code; print commerce_currency_format($total_amount,$currency_code);
    – user49951
    Commented Oct 16, 2015 at 4:51
  • $line_item=commerce_order_load($order_id); $total_amount=$line_item->commerce_order_total['und'][0]['amount']; $currency_code=$line_item->commerce_order_total['und'][0]['currency_code']; print commerce_currency_format($total_amount,$currency_code);
    – user49951
    Commented Oct 16, 2015 at 5:55
0

Floats are inherently inaccurate, and thus unsuitable to use for money. Operations on accurate decimal types are very slow, and there is no guarantee all databases will do them right, some might use floats internally anyway, and only pretend. And money format is not popular, and is known to be sometimes implemented in a buggy way in some database engines. That leaves integers.

All you need to do is divide by 100 before you use it, and multiply by 100 before save. That's all, no more philosophy behind it.

0

You can always use:

commerce_currency_amount_to_decimal($amount, $currency_code);

$amount: The price to convert to a decimal value.

$currency_code: The currency code of the price whose decimals value will be used to divide by the proper divisor when converting the amount.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.