You are here

class CommerceShippingCurrency in Commerce Currency Resolver 8

Handling shipments rates.

@package Drupal\commerce_currency_resolver\EventSubscriber

Hierarchy

Expanded class hierarchy of CommerceShippingCurrency

1 string reference to 'CommerceShippingCurrency'
commerce_currency_resolver_shipping.services.yml in modules/shipping/commerce_currency_resolver_shipping.services.yml
modules/shipping/commerce_currency_resolver_shipping.services.yml
1 service uses CommerceShippingCurrency
commerce_currency_resolver_shipping.rates in modules/shipping/commerce_currency_resolver_shipping.services.yml
Drupal\commerce_currency_resolver_shipping\EventSubscriber\CommerceShippingCurrency

File

modules/shipping/src/EventSubscriber/CommerceShippingCurrency.php, line 17

Namespace

Drupal\commerce_currency_resolver_shipping\EventSubscriber
View source
class CommerceShippingCurrency implements EventSubscriberInterface {
  use CommerceCurrencyResolversRefreshTrait;

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

  /**
   * Exchanger calculator.
   *
   * @var \Drupal\commerce_exchanger\ExchangerCalculatorInterface
   */
  protected $priceExchanger;

  /**
   * {@inheritdoc}
   */
  public function __construct(CurrentCurrency $currency, ExchangerCalculatorInterface $price_exchanger) {
    $this->currentCurrency = $currency;
    $this->priceExchanger = $price_exchanger;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [
      'commerce_shipping.rates' => 'shippingCurrency',
    ];
    return $events;
  }

  /**
   * Calculate rates based on current currency.
   *
   * @param \Drupal\commerce_shipping\Event\ShippingRatesEvent $event
   *   The order event.
   */
  public function shippingCurrency(ShippingRatesEvent $event) {

    /** @var \Drupal\commerce_shipping\ShippingRate[] $rates */
    if ($rates = $event
      ->getRates()) {
      if ($this
        ->isShippingAdminRoute()) {
        $resolved_currency = $event
          ->getShipment()
          ->getAmount()
          ->getCurrencyCode();
      }
      else {
        $resolved_currency = $this->currentCurrency
          ->getCurrency();
      }
      $shipping_conversion = [];
      foreach ($rates as $key => $rate) {
        if ($rate
          ->getAmount()
          ->getCurrencyCode() != $resolved_currency) {
          $plugin_data = $event
            ->getShippingMethod()
            ->getPlugin()
            ->getConfiguration();

          // Check if they are entered separate prices per currencies.
          if (isset($plugin_data['fields'][$resolved_currency]) && is_numeric($plugin_data['fields'][$resolved_currency]['number'])) {
            $resolved_price = new Price((string) $plugin_data['fields'][$resolved_currency]['number'], $plugin_data['fields'][$resolved_currency]['currency_code']);
            $rate
              ->setOriginalAmount($resolved_price);
            $rate
              ->setAmount($resolved_price);
          }
          else {
            $rate
              ->setAmount($this->priceExchanger
              ->priceConversion($rate
              ->getAmount(), $resolved_currency));
            $rate
              ->setOriginalAmount($this->priceExchanger
              ->priceConversion($rate
              ->getOriginalAmount(), $resolved_currency));
          }
          $shipping_conversion[$key] = $rate;
        }
      }
      if ($shipping_conversion) {
        $event
          ->setRates($shipping_conversion);
      }
    }
  }

}

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.
CommerceShippingCurrency::$currentCurrency protected property Current currency.
CommerceShippingCurrency::$priceExchanger protected property Exchanger calculator.
CommerceShippingCurrency::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
CommerceShippingCurrency::shippingCurrency public function Calculate rates based on current currency.
CommerceShippingCurrency::__construct public function