You are here

public function CurrencyOrderRefresh::checkCurrency in Commerce Currency Resolver 8

Check for misplace in currency. Refresh order if necessary.

Parameters

\Drupal\commerce_order\Event\OrderEvent $event: The order event.

File

src/EventSubscriber/CurrencyOrderRefresh.php, line 68

Class

CurrencyOrderRefresh
Checking for mismatch in currencies on order.

Namespace

Drupal\commerce_currency_resolver\EventSubscriber

Code

public function checkCurrency(OrderEvent $event) {
  $orders =& drupal_static(__FUNCTION__, []);

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $event
    ->getOrder();

  // We already have fired this event.
  if (isset($orders[$order
    ->id()])) {
    return;
  }

  // Get order total currency.
  if ($this
    ->shouldCurrencyRefresh($order) && ($order_total = $order
    ->getTotalPrice())) {
    $order_currency = $order_total
      ->getCurrencyCode();
    $resolved_currency = $this->currentCurrency
      ->getCurrency();

    // Compare order total and main resolved currency.
    // Refresh order if they are different. We need then alter total price.
    // This will trigger order processor which will handle
    // correction of total order price and currency.
    if ($order_currency !== $resolved_currency) {

      // Set as flag to trigger this even once.
      $orders[$order
        ->id()] = TRUE;

      // Check if we can refresh order.
      $this->orderRefresh
        ->refresh($order);
    }
  }
}