You are here

class DonateOrderProcessor in Commerce Donate 8

Adjust Currency of Donation when Order Changes currency.

Hierarchy

Expanded class hierarchy of DonateOrderProcessor

1 string reference to 'DonateOrderProcessor'
commerce_donate.services.yml in ./commerce_donate.services.yml
commerce_donate.services.yml
1 service uses DonateOrderProcessor
commerce_donate.order_processor in ./commerce_donate.services.yml
Drupal\commerce_donate\DonateOrderProcessor

File

src/DonateOrderProcessor.php, line 18

Namespace

Drupal\commerce_donate
View source
class DonateOrderProcessor implements OrderProcessorInterface {
  use CommerceCurrencyResolversRefreshTrait;

  /**
   * The order storage.
   *
   * @var \Drupal\commerce_order\OrderStorage
   */
  protected $orderStorage;

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

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

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * {@inheritdoc}
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, CurrentCurrency $currency, AccountInterface $account, RouteMatchInterface $route_match) {
    $this->orderStorage = $entity_type_manager
      ->getStorage('commerce_order');
    $this->routeMatch = $route_match;
    $this->account = $account;
    $this->currentCurrency = $currency;
  }

  /**
   * {@inheritdoc}
   */
  public function process(OrderInterface $order) {

    // Try to find an existing order item.
    foreach ($order
      ->getItems() as $order_item) {
      if ($order_item
        ->bundle() == 'donation') {
        $donation_order_item = $order_item;
        break;
      }
    }
    if (isset($donation_order_item)) {

      // Get order total.
      $total = $order
        ->getTotalPrice();

      // Get main currency.
      $resolved_currency = $this->currentCurrency
        ->getCurrency();

      // This is used only to ensure that order have resolved currency.
      // In combination with check on order load we can ensure that currency is
      // same accross entire order.
      // This solution provides avoiding constant recalculation
      // on order load event on currency switch (if we don't explicitly set
      // currency for total price when we switch currency.
      // @see \Drupal\commerce_currency_resolver\EventSubscriber\OrderCurrencyRefresh
      if ($total
        ->getCurrencyCode() !== $resolved_currency && $this
        ->shouldCurrencyRefresh($order)) {
        $amount = $donation_order_item
          ->getUnitPrice()
          ->getNumber();
        $donation_order_item->unit_price = [
          'number' => $amount,
          'currency_code' => $resolved_currency,
        ];
        $currency_formatter = \Drupal::service('commerce_price.currency_formatter');
        $amount_label = $currency_formatter
          ->format($amount, $resolved_currency);
        $donation_order_item->title = t('@amount donation', [
          '@amount' => $amount_label,
        ]);
        $donation_order_item
          ->save();

        // Save order.
        $order
          ->save();
      }
    }
  }

}

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.
DonateOrderProcessor::$account protected property The current user.
DonateOrderProcessor::$currentCurrency protected property Current currency.
DonateOrderProcessor::$orderStorage protected property The order storage.
DonateOrderProcessor::$routeMatch protected property The current route match.
DonateOrderProcessor::process public function Processes an order. Overrides OrderProcessorInterface::process
DonateOrderProcessor::__construct public function