To simplify the daily management of an e-commerce store, it can be useful to display prices excluding VAT (net prices) directly in the administrative views, especially to ease accounting calculations on a day-to-day basis.
By default, Drupal Commerce displays prices including VAT (gross prices) in its views and does not offer an option to show only the net price.
This article proposes a solution based on the development of a custom formatter that modifies how prices are displayed in views, specifically to show prices excluding VAT.
Logic Behind the Module
The module is based on a custom Drupal formatter for fields of type commerce_price
, which changes the display of prices by calculating the net price from the gross price and the VAT rate to apply.
It uses adjustments, which are data attached to order items that allow applying taxes, additional fees, promotions, etc.
Creating a Custom Formatter
To create a custom Drupal formatter, the easiest approach is to use the generator provided by Drush with the drush generate field-formatter
command.
This will guide you through the necessary questions to generate the module files for your formatter without writing a single line of code.
Once the formatter is created, you can edit the file located in src/Plugin/Field/FieldFormatter
and start developing it.
In my case, I generated a formatter via Drush named ExcludingVatPriceFormatter.php
.
To apply this formatter to fields of type commerce_price
, simply declare its type in the class annotation like this:
/**
* Plugin implementation of the 'Excluding VAT Price Formatter' formatter.
*
* @FieldFormatter(
* id = "excluding_vat_price_formatter",
* label = @Translation("Excluding VAT Price Formatter"),
* field_types = {"commerce_price"},
* )
*/
Next, we loop through the items to format and retrieve the VAT rate to apply (the highest rate is used if multiple rates are declared, which you can adjust as needed).
Once the rate is obtained, we use it to calculate the net price, format the price using Drupal Commerce’s commerce_price.currency_formatter
service, and return the element.
Here’s the complete code of my formatter:
<?php
declare(strict_types=1);
namespace Drupal\excluding_vat_price_formatter\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\commerce_price\Plugin\Field\FieldFormatter\PriceDefaultFormatter;
/**
* Plugin implementation of the 'Excluding VAT Price Formatter' formatter.
*
* @FieldFormatter(
* id = "excluding_vat_price_formatter",
* label = @Translation("Excluding VAT Price Formatter"),
* field_types = {"commerce_price"},
* )
*/
final class ExcludingVatPriceFormatter extends PriceDefaultFormatter {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$price = $item->toPrice();
$order_item = $item->getEntity();
$final_vat_factor = 1;
if ($order_item && $order_item->hasField('adjustments')) {
// Use collectAdjustments when available (order summary page)
if (method_exists($order_item, 'collectAdjustments')) {
$adjustments = $order_item->collectAdjustments();
} else {
$adjustments = $order_item->getAdjustments();
}
$vat_factors = [];
foreach ($adjustments as $adjustment) {
if ($adjustment->getType() === 'tax') {
$vat_factors[] = 1 + $adjustment->getPercentage();
}
}
if (!empty($vat_factors)) {
// Use the highest VAT factor if multiple are present.
$final_vat_factor = max($vat_factors);
}
}
// Calculate the price excluding VAT.
$price_excluding_vat = $price->divide((string) $final_vat_factor);
// Format the price.
$currency_formatter = \Drupal::service('commerce_price.currency_formatter');
$formatted_price = $currency_formatter->format(
$price_excluding_vat->getNumber(),
$price_excluding_vat->getCurrencyCode()
);
$elements[$delta] = ['#markup' => $formatted_price];
}
return $elements;
}
}
Using the Custom Formatter
Once your formatter is complete, don't forget to enable the module you just created from the Extensions tab in your Drupal installation or directly with Drush using the command drush en excluding_vat_price_formatter
(or the system name of your custom module).
You can then manage your Views via Structure > Views, and modify the field settings for price fields in the administrative views to use your custom formatter and display prices excluding VAT. Don’t forget to also modify the label of your fields to something like "Unit Price (Excl. VAT)" to clarify that the prices are shown excluding VAT and not including it.