Skip to content

Instantly share code, notes, and snippets.

@BBGuy
Last active August 29, 2015 14:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BBGuy/11259684 to your computer and use it in GitHub Desktop.
Save BBGuy/11259684 to your computer and use it in GitHub Desktop.
Drupal Commerce Order Total components
<?php
// Get a nicely formatted amount for Ex VAT & Vat order totals.
// Get the order wrapper
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
// Order total.
$order_total = $order_wrapper->commerce_order_total->value();
// Get ex vat amount.
$total_ex_vat = commerce_price_component_total($order_total , 'base_price');
$total_ex_vat = commerce_currency_format($total_ex_vat['amount'], $total_ex_vat['currency_code']);
// Get vat amount.
$total_vat = commerce_price_component_total($order_total , 'tax|vat');
$total_vat = commerce_currency_format($total_vat['amount'], $total_vat['currency_code']);
?>
<?php
// Get unformatted amount for Ex VAT & Vat order totals.
// This is not ideal as country specific rounding issues may result in inaccurate amounts (see next file for best appproach)
// Get the order wrapper
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
// Order total.
$order_total = $order_wrapper->commerce_order_total->value();
// Get ex vat amount.
$total_ex_vat = commerce_price_component_total($order_total , 'base_price');
$amount = commerce_currency_amount_to_decimal($total_ex_vat['amount'], $total_ex_vat['currency_code']);
$total_ex_vat = $amount;
// Get vat amount.
$total_vat = commerce_price_component_total($order_total , 'tax|vat');
$amount = commerce_currency_amount_to_decimal($total_vat['amount'], $total_vat['currency_code']);
$total_vat = $amount;
?>
<?php
// Get unformatted amount for Ex VAT & Vat order totals with correct country specific rounding and formatting.
// Get the order wrapper
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
// Order total.
$order_total = $order_wrapper->commerce_order_total->value();
// Get ex vat amount.
$total_ex_vat = commerce_price_component_total($order_total , 'base_price');
// Get the decimal amount.
$amount = commerce_currency_amount_to_decimal($total_ex_vat['amount'], $total_ex_vat['currency_code']);
// Load the currency object.
$currency = commerce_currency_load($total_ex_vat['currency_code']);
// Round and format.
$total_ex_vat = number_format(commerce_currency_round($amount, $currency), $currency['decimals'], $currency['decimal_separator'], $currency['thousands_separator']);
// Get vat amount.
$total_vat = commerce_price_component_total($order_total , 'tax|vat');
// Get the decimal amount.
$amount = commerce_currency_amount_to_decimal($total_vat['amount'], $total_vat['currency_code']);
// Load the currency object.
$currency = commerce_currency_load($total_vat['currency_code']);
// Round and format.
$total_vat = number_format(commerce_currency_round($amount, $currency), $currency['decimals'], $currency['decimal_separator'], $currency['thousands_separator']);
?>
@BBGuy
Copy link
Author

BBGuy commented Nov 18, 2014

the above examples work if you know the tax type otherwise see https://gist.github.com/BBGuy/2fae6d30a39a6cc00109

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment