03 328 8646

Blog

Drupal Commerce 2.x Notes & code snippets

Unless you know you want to be reading this you probably don't! It's currently a dumping ground for Commerce 2.x notes and code that we've come across and are likely to need again some time, maybe you need them too?

 

Formatting prices in code:

Use the

NumberFormatterFactoryInterface $number_formatter_factory to create a numberFormatter instance:
$this->numberFormatter = $number_formatter_factory->createInstance();

Then set the details you want:

$this->numberFormatter = $number_formatter_factory->createInstance();
$this->numberFormatter->setMaximumFractionDigits(2);
$this->numberFormatter->setMinimumFractionDigits(2);
$this->numberFormatter->setCurrencyDisplay(NumberFormatterInterface::CURRENCY_DISPLAY_CODE);