We all know that Drupal Commerce is really flexible. If you want variable pricing dependant upon location, csutomer type or basket size, no problem. However, some areas of Drupal Commerce do require not only relatively specialist knowledge, but also coding to give the required outcome. We recently had an interesting problem through Cambridge Digital Academy, wherein they were looking at displaying discounts at the checkout form. By default the order of display is:

  • Product: £10.00
  • Subtotal: £7.00
  • VAT: £1.67
  • Discount: £3.00
  • Total: £7.00

By default that makes a relatively difficult to work out checkout experience. And we wanted the weighting to be a lot more simple.

  • Product: £10.00
  • Discount: £3.00
  • Subtotal: £7.00
  • VAT: £1.67
  • Total: £7.00

However, that is not necessarily an easy task, at least there are no options to be able to configure the price formatters from the frontend. You have to write code. The good news is that the code is fairly simple and just makes changes to weight in order to re-order, wherein the less the weight the higher it appears. This managed to give us the required output!

/**
 * Implements hook_commerce_price_formatted_components_alter(&$components, $price, $entity).
 */
function camtech_commerce_price_formatted_components_alter(&$components, $price, $entity) {
  $components['discount|discount_early_bird']['weight'] = 0;
  $components['base_price']['weight'] = 1;
  $components['tax|vat']['weight'] = 2;
  $components['commerce_price_formatted_amount']['weight'] = 10;
}