How to edit manual rates programmatically

Created on 16 April 2023, over 1 year ago
Updated 1 May 2024, 7 months ago

Problem/Motivation

I would like to update the "manual" exchange rates from other parts of my site, for example with ECA processes, Feeds, other forms in general.
Is it possible? How to? 🙏🏻

💬 Support request
Status

Closed: outdated

Version

1.0

Component

Documentation

Created by

🇮🇹Italy kopeboy Milan

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @kopeboy
  • 🇭🇷Croatia valic Osijek

    If you create a manual exchange rate, let's say, with machine name example,

    you then get the configuration file which is exported and named
    commerce_exchanger.latest_exchange_rates.example.yml

    Let's say you manually defined the relationship on creation between HRK and EUR currency.
    The above YML configuration file then have contents as shown here:

    rates:
      EUR:
        HRK:
          value: 7.5345
          sync: 1
      HRK:
        EUR:
          value: 0.1327
          sync: 1
    

    Programmatically you can modify these by using the config service.

    Alter ratio EUR / HRK from 7.5345 to 1000 would look something like this:

      $rates = \Drupal::configFactory()->getEditable('commerce_exchanger.latest_exchange_rates.example');
      $rates->set('EUR', ['HRK' => ['value' => 1000, 'sync' => 1]]);
      $rates->save();
    

    But as well you can use UI in Drupal to alter values.

    If you have an external rate source, I would suggest creating a plugin.
    Usually, this boils down to a few lines of code (see examples in the main module or readme)

  • Status changed to Closed: outdated 7 months ago
  • 🇭🇷Croatia valic Osijek

    #2 is no longer applicable, from version 2 we don't use any more Drupal configuration to store exchange rates.

    Now it's dedicated database table `commerce_exchanger_latest_rates`

    See 3366705 🐛 Latest exchange rates shouldn't be saved in config Fixed

    You can use https://git.drupalcode.org/project/commerce_exchanger/-/blob/2.0.x/src/E... for storing and retrieving data.

Production build 0.71.5 2024