-
-
Save BBGuy/2fae6d30a39a6cc00109 to your computer and use it in GitHub Desktop.
A function to extract the price from a Drupal commerce line item.
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 | |
function get_line_item_price($line_item_wrapper) { | |
$tax_amount = 0; | |
$base_price = 0; | |
$unit_price = 0; | |
$unit_price_component = $line_item_wrapper->commerce_unit_price->value(); | |
if ($unit_price_component) { | |
$unit_price = $unit_price_component['amount']; | |
// calculate included tax amount on base amount. | |
foreach ($unit_price_component['data']['components'] as $component) { | |
if (!empty($component['included']) && !empty($component['price']['data']['tax_rate'])) { | |
$tax_amount += $component['price']['amount']; | |
} | |
} | |
// Get the base price same as ex_vat if no other components. | |
$base_price_component = commerce_price_component_total($unit_price_component, 'base_price'); | |
//$base_currency = commerce_currency_load($base_price_component['currency_code']); | |
if ($base_price_component) { | |
$base_price = $base_price_component['amount']; | |
} | |
} | |
return array ( | |
'inc_vat' => $unit_price, | |
'ex_vat' => ($unit_price - $tax_amount), | |
'vat' => $tax_amount, | |
'base_price' => $base_price, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment