Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Loading...
Applies to versions: 14 and higher

Currencies

Recommended reading 
Note: This documentation is for the preview version of the upcoming release of CMS 12/Commerce 14/Search & Navigation 14. Features included here might not be complete, and might be changed before becoming available in the public release. This documentation is provided for evaluation purposes only.

This topic explains how to configure currencies used when configuring markets and placing orders.  On install, a complete list of paper currencies is created.

Classes in this topic are in the following namespaces: 

  • Mediachase.Commerce.Catalog.Dto
  • Mediachase.Commerce.Catalog.Managers

Working with currencies

Creating currencies

var existingCurrencies = CurrencyManager.GetCurrencyDto();
var dto = new CurrencyDto();
var currencyRow = dto.Currency.NewCurrencyRow();
currencyRow.Name = "Bitcoin";
currencyRow.CurrencyCode = "BTC";
currencyRow.ModifiedDate = DateTime.UtcNow;

if (currencyRow.RowState == DataRowState.Detached)
    dto.Currency.Rows.Add(currencyRow);

var rateRow = dto.CurrencyRate.NewCurrencyRateRow();
rateRow.FromCurrencyId = dto.Currency[0].CurrencyId;
rateRow.ToCurrencyId = existingCurrencies.Currency.First(x => x.CurrencyCode.Equals("USD")).CurrencyId;
rateRow.EndOfDayRate = 50000;
rateRow.AverageRate = 50000;
rateRow.CurrencyRateDate = DateTime.Now.Date;
rateRow.ModifiedDate = DateTime.UtcNow;

if (rateRow.RowState == DataRowState.Detached)
    dto.CurrencyRate.AddCurrencyRateRow(rateRow);

CurrencyManager.SaveCurrency(dto);

Updating currencies

var dto = CurrencyManager.GetCurrencyDto();
var currencyRow = dto.Currency.FirstOrDefault(x => x.CurrencyCode.Equals("USD"));
currencyRow.Name = "USA Dollar";
currencyRow.ModifiedDate = DateTime.UtcNow;
var rateRow = currencyRow.GetCurrencyRateRows().FirstOrDefault(x => x.FromCurrencyId == dto.Currency.FirstOrDefault(x => x.CurrencyCode.Equals("BTC")).CurrencyId);
rateRow.EndOfDayRate = 55000;
rateRow.AverageRate = 49000;
rateRow.CurrencyRateDate = DateTime.Now.Date;
rateRow.ModifiedDate = DateTime.UtcNow;
CurrencyManager.SaveCurrency(dto);

Deleting currencies

var dto = CurrencyManager.GetCurrencyDto();
var currencyRow = dto.Currency.FirstOrDefault(x => x.CurrencyCode.Equals("USD"));
currencyRow.Delete();
CurrencyManager.SaveCurrency(dto);
Do you find this information helpful? Please log in to provide feedback.

Last updated: Jul 02, 2021

Recommended reading