Formatting prices

There are various ways to display prices. Drupal Commerce provides several price field formatters and a Twig filter.

Rendering prices in Twig.

There is the commerce_price_format Twig filter which renders a given Price value object based on its currency definition. Here is an example of using the Twig filter by utilizing the inline_template element type.

$element['price'] = [
  '#type' => 'inline_template',
  '#template' => '{{ price|commerce_price_format }}',
  '#context' => [
    'price' => new \Drupal\commerce_price\Price('10.25', 'USD'),
  ],
];

This would render as $10.25.

Field formatters

Plain

Prints the price using the commerce_price_plain theme hook. Resembles 10.25 USD. Can be useful for exported data (CSV).

Default

Uses the number formatter service to return marked up output. Same output as Twig filter.

Calculated price

Returns price formatted for its currency, however it uses price resolving and can take into consideration other adjustments (promotions.)

Found errors? Think you can improve this documentation? edit this page