You are here

class CurrencyOrderRefresh in Commerce Currency Resolver 8

Checking for mismatch in currencies on order.

@package Drupal\commerce_currency_resolver\EventSubscriber

Hierarchy

Expanded class hierarchy of CurrencyOrderRefresh

1 string reference to 'CurrencyOrderRefresh'
commerce_currency_resolver.services.yml in ./commerce_currency_resolver.services.yml
commerce_currency_resolver.services.yml
1 service uses CurrencyOrderRefresh
commerce_currency_resolver.order_currency in ./commerce_currency_resolver.services.yml
Drupal\commerce_currency_resolver\EventSubscriber\CurrencyOrderRefresh

File

src/EventSubscriber/CurrencyOrderRefresh.php, line 18

Namespace

Drupal\commerce_currency_resolver\EventSubscriber
View source
class CurrencyOrderRefresh implements EventSubscriberInterface {
  use CommerceCurrencyResolversRefreshTrait;

  /**
   * The order refresh.
   *
   * @var \Drupal\commerce_order\OrderRefreshInterface
   */
  protected $orderRefresh;

  /**
   * Current currency.
   *
   * @var \Drupal\commerce_currency_resolver\CurrentCurrencyInterface
   */
  protected $currentCurrency;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $account;

  /**
   * {@inheritdoc}
   */
  public function __construct(CurrentCurrency $currency, OrderRefreshInterface $order_refresh, AccountInterface $account) {
    $this->account = $account;
    $this->currentCurrency = $currency;
    $this->orderRefresh = $order_refresh;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [
      'commerce_order.commerce_order.load' => 'checkCurrency',
    ];
    return $events;
  }

  /**
   * Check for misplace in currency. Refresh order if necessary.
   *
   * @param \Drupal\commerce_order\Event\OrderEvent $event
   *   The order event.
   */
  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);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommerceCurrencyResolversRefreshTrait::checkOrderOwner public function Check if order belongs to current user.
CommerceCurrencyResolversRefreshTrait::checkOrderStatus public function Check if order is in draft status.
CommerceCurrencyResolversRefreshTrait::isAdminPath public function Detect admin/* paths.
CommerceCurrencyResolversRefreshTrait::isShippingAdminRoute public function Check shipping admin route.
CommerceCurrencyResolversRefreshTrait::shouldCurrencyRefresh public function Get refresh state based on path.
CurrencyOrderRefresh::$account protected property The current user.
CurrencyOrderRefresh::$currentCurrency protected property Current currency.
CurrencyOrderRefresh::$orderRefresh protected property The order refresh.
CurrencyOrderRefresh::checkCurrency public function Check for misplace in currency. Refresh order if necessary.
CurrencyOrderRefresh::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
CurrencyOrderRefresh::__construct public function