You are here

public function CommerceCurrencyResolver::resolve in Commerce Currency Resolver 8

Resolves a price for the given purchasable entity.

Use $context->getData('field_name', 'price') to get the name of the field for which the price is being resolved (e.g "list_price", "price").

Parameters

\Drupal\commerce\PurchasableEntityInterface $entity: The purchasable entity.

string $quantity: The quantity.

\Drupal\commerce\Context $context: The context.

Return value

\Drupal\commerce_price\Price|null A price value object, if resolved. Otherwise NULL, indicating that the next resolver in the chain should be called.

Overrides PriceResolverInterface::resolve

File

src/Resolver/CommerceCurrencyResolver.php, line 60

Class

CommerceCurrencyResolver
Returns a price and currency depending of language or country.

Namespace

Drupal\commerce_currency_resolver\Resolver

Code

public function resolve(PurchasableEntityInterface $entity, $quantity, Context $context) {

  // Check if we need to skip resolving prices.
  if (!$this
    ->skipResolvingPrice()) {

    // Default price.
    $price = NULL;

    // Get field from context.
    $field_name = $context
      ->getData('field_name', 'price');

    // @see \Drupal\commerce_price\Resolver\DefaultPriceResolver
    if ($field_name === 'price') {
      $price = $entity
        ->getPrice();
    }
    elseif ($entity
      ->hasField($field_name) && !$entity
      ->get($field_name)
      ->isEmpty()) {
      $price = $entity
        ->get($field_name)
        ->first()
        ->toPrice();
    }

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

    // If we have price, and the resolved price currency is different than the
    // current currency.
    if ($price && $resolved_currency !== $price
      ->getCurrencyCode()) {

      // Get how price should be calculated.
      $currency_source = $this->configFactory
        ->get('commerce_currency_resolver.settings')
        ->get('currency_source');
      $resolved_price = NULL;

      // Specific cases for field and combo. Even we had auto-calculated
      // price, in combo mode we could have field with price.
      if ($currency_source === 'combo' || $currency_source === 'field') {

        // Backward compatibility for older version, and inital setup
        // that default price fields are mapped to field_price_currency_code
        // instead to price_currency_code.
        if ($field_name === 'price') {
          $field_name = 'field_price';
        }
        $resolved_field = $field_name . '_' . strtolower($resolved_currency);

        // Check if we have field.
        if ($entity
          ->hasField($resolved_field) && !$entity
          ->get($resolved_field)
          ->isEmpty()) {
          $resolved_price = $entity
            ->get($resolved_field)
            ->first()
            ->toPrice();
        }
      }

      // If we haven't resolved yet anything, auto-calculate price by default.
      // Fallback for all cases regardless of chosen currency source mode.
      if ($resolved_price === NULL) {
        $resolved_price = $this->priceExchanger
          ->priceConversion($price, $resolved_currency);
      }
      return $resolved_price;
    }
  }
}