Comments

Ahmed_Samir created an issue. See original summary.

ahmed_samir’s picture

Status: Active » Needs review
StatusFileSize
new1.7 KB

Attaching the patch.

Status: Needs review » Needs work
valic’s picture

Can you reproduce steps for this bug?

Currently, default price calculator which comes with Commerce Exchanger is having the following logic:
\Drupal\commerce_exchanger\DefaultExchangerCalculator

inside we have

   /**
   * {@inheritdoc}
   */
  public function getExchangerId() {
    // Return the first one.
    foreach ($this->providers as $provider) {
      if ($provider->status()) {
        return $provider->getExchangerConfigName();
      }
    }

    return '';
  }

and inside AbstractClass we have

  public function priceConversion(Price $price, string $target_currency) {
    $exchange_rates_config = $this->getExchangerId();

    if (empty($exchange_rates_config)) {
      throw new ExchangeRatesDataMismatchException('Not any active Exchange rates present');
    }

So by default it there is no published exchange rates ($this->getExchangerId()) you should get exception

valic’s picture

Not sure why I have decided to return an empty string, it should be more correct to return maybe a NULL.

Something like this.

  public function getExchangerId() {
    // Return the first one.
    foreach ($this->providers as $provider) {
      if ($provider->status()) {
        return $provider->getExchangerConfigName();
      }
    }

    return NULL;
  }
valic’s picture

Ah, so the issue is in currency resolver which does not take into account status.

Commerce exchanger by default does not perform conversion if there are no enabled exchanger plugins.

There is a simpler solution for that, but wanted to avoid constant loading results vs. getting from a cache

inside constructor we have

$this->providers = $entity_type_manager->getStorage('commerce_exchange_rates')->loadMultiple();

this would solve any problem, but loadByProperties is no cached
$this->providers = $entity_type_manager->getStorage('commerce_exchange_rates')->loadByProperties(['status' => 1]);

valic’s picture

Status: Needs work » Closed (works as designed)

I saw your patch on Currency Resolver, there is a place to fix this, while it's caused by the resolver module, not the exchanger

ahmed_samir’s picture

Hi Valic, this is technically not a bug. the way we have it setup right now is that we have a manual exchanger which we don't really use but in order to use commerce_currency_resolver module we have to have this plugin otherwise we would get this 'Not any active Exchange rates present' exception.

After some debugging it turns out that getExchangerId() was getting called from PriceExchangerCalculator not DefaultExchangerCalculator that's why I created this patch to be able to skip the price conversion if the exchanger is disabled.

ahmed_samir’s picture

Commerce exchanger by default does not perform conversion if there are no enabled exchanger plugins.

Could you please point out where this check is?

is there any other places than this?


    if (empty($exchange_rates_config)) {
      throw new ExchangeRatesDataMismatchException('Not any active Exchange rates present');
    }

valic’s picture

So you want to use resolver, but you don't need to exchange rates. Let say you set all fields on prices and you are happy with that.

But still, a lot of things (adjustments, etc..) would need an automatic conversion of rates, otherwise, you will have an order with multiple different currencies, and commerce would not allow you to proceed.

As well, if you don't need exchange rates, for what you are using resolver? Which criteria are you using, geo, cookie, or?

ahmed_samir’s picture

StatusFileSize
new78.32 KB

We're using the cookie option for the resolver along with the price field per currency as follows:

field_price_usd
field_price_aud
field_price_eur
field_price_cad
field_price_gbp

And we provide our own pricing so, we don't need the exchanger but, we have to use a disabled manual exchanger to be able to use the resolver. attached pic of the settings when the exchanger was deleted:

settings

It would be nice if there is an option to use the resolver without the exchanger.

valic’s picture

Status: Closed (works as designed) » Active

Opening again this issue.

So based on your (and possibly other's people scenarios) when you have all the prices, it would make sense to adjust the default calculator

instead of

 public function priceConversion(Price $price, string $target_currency) {
    $exchange_rates_config = $this->getExchangerId();

    if (empty($exchange_rates_config)) {
      throw new ExchangeRatesDataMismatchException('Not any active Exchange rates present');
    }

    // Price currency.
    $price_currency = $price->getCurrencyCode();

    // If someone is trying to convert same currency.
    if ($price_currency == $target_currency) {
      return $price;
    }


to swap what comes first

 public function priceConversion(Price $price, string $target_currency) {
  // Price currency.
    $price_currency = $price->getCurrencyCode();

    // If someone is trying to convert same currency.
    if ($price_currency == $target_currency) {
      return $price;
    }

    $exchange_rates_config = $this->getExchangerId();

    if (empty($exchange_rates_config)) {
      throw new ExchangeRatesDataMismatchException('Not any active Exchange rates present');
    }

But still, dependency for commerce_exchanger would be on resolver, while it needs to be.

Let say your cookie scenario:
Cookie A = USD
Cookie B = EUR

The user has cookie A and has order in the cart, then he switches to cookie B.
The order needs to be refreshed then, and you will need to have proper exchange rates because some part of the order you can't define without doing currency conversion/calculation like adjustments. Think about it as a fail-safe mechanism

Without exchange rates, it is going to break order.

So maybe there would be a option to use resolve with exchange rates, but not before 2x version of that module

valic’s picture

Closing this one.

The exchanger is working as it should. This can be moved to the resolver module

valic’s picture

Just to notify you, that there is now a general issue about having exchanger as required in some cases

https://www.drupal.org/project/commerce_currency_resolver/issues/3150218

rajivgandhi chinnakrishnan’s picture

The patch provided in comment #2 does not account for the "commerce_currency_resolver.settings" configuration values. We've addressed this issue and updated the patch accordingly.

The below patch from "commerce_currency_resolver" also required to resolve this issue.
https://www.drupal.org/project/commerce_currency_resolver/issues/3150218...