--- a/commerce_cart_advanced.services.yml +++ b/commerce_cart_advanced.services.yml @@ -50,7 +50,7 @@ services: class: 'Drupal\commerce_cart_advanced\Hook\CartItemFormAlter' arguments: - '@entity_type.manager' - - '@commerce_price.number_formatter_factory' + - '@commerce_price.currency_formatter' - '@current_route_match' - '@string_translation' --- a/src/Hook/CartItemFormAlter.php +++ b/src/Hook/CartItemFormAlter.php @@ -2,9 +2,9 @@ namespace Drupal\commerce_cart_advanced\Hook; +use CommerceGuys\Intl\Formatter\CurrencyFormatterInterface; use Drupal\commerce_order\Entity\OrderInterface; use Drupal\commerce_order\Entity\OrderItemInterface; -use Drupal\commerce_price\NumberFormatterFactoryInterface; use Drupal\commerce_price\Price; use Drupal\Core\Entity\ContentEntityFormInterface; @@ -51,9 +51,9 @@ class CartItemFormAlter { /** * The number formatter. * - * @var \CommerceGuys\Intl\Formatter\NumberFormatterInterface + * @var \CommerceGuys\Intl\Formatter\CurrencyFormatterInterface */ - protected $numberFormatter; + protected $currencyFormatter; /** * The current route match service. @@ -67,7 +67,7 @@ class CartItemFormAlter { * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. - * @param \Drupal\commerce_price\NumberFormatterFactoryInterface $number_formatter_factory + * @param \CommerceGuys\Intl\Formatter\CurrencyFormatterInterface $currency_formatter * The number formatter factory. * @param \Drupal\Core\Routing\RouteMatchInterface $current_route_match * The current route match service. @@ -76,7 +76,7 @@ class CartItemFormAlter { */ public function __construct( EntityTypeManagerInterface $entity_type_manager, - NumberFormatterFactoryInterface $number_formatter_factory, + CurrencyFormatterInterface $currency_formatter, RouteMatchInterface $current_route_match, TranslationInterface $string_translation ) { @@ -89,8 +89,7 @@ class CartItemFormAlter { // priority : low // notes : Could be configurable order item type. Should include // current display and minimum fraction digits. - $this->numberFormatter = $number_formatter_factory->createInstance(); - $this->numberFormatter->setMaximumFractionDigits(2); + $this->currencyFormatter = $number_formatter_factory; } /** @@ -347,7 +346,7 @@ class CartItemFormAlter { ->load($price->getCurrencyCode()); return [ - '#markup' => $this->numberFormatter->formatCurrency( + '#markup' => $this->currencyFormatter->formatCurrency( $price->getNumber(), $currency ),