Last active
August 29, 2015 14:00
-
-
Save BBGuy/11259684 to your computer and use it in GitHub Desktop.
Drupal Commerce Order Total components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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']); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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']); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the above examples work if you know the tax type otherwise see https://gist.github.com/BBGuy/2fae6d30a39a6cc00109