Skip to content

Instantly share code, notes, and snippets.

@BBGuy
Last active January 17, 2018 12:44
Show Gist options
  • Save BBGuy/2fae6d30a39a6cc00109 to your computer and use it in GitHub Desktop.
Save BBGuy/2fae6d30a39a6cc00109 to your computer and use it in GitHub Desktop.
A function to extract the price from a Drupal commerce line item.
<?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