Implementing a Currency Switcher
Learn how to implement a currency switcher in Umbraco Commerce.
Last updated
Was this helpful?
Learn how to implement a currency switcher in Umbraco Commerce.
Last updated
Was this helpful?
In a globalized world, it is essential to provide users with the ability to switch between different currencies. This feature is especially important for e-commerce websites that cater to customers from different countries.
In this guide, you can learn how to implement a currency switcher in Umbraco Commerce.
Define the countries and currencies you want to support, in the Umbraco backoffice.
Navigate to the Content section.
Populate the product prices for each currency.
A partial view is used on the frontend to allow users to toggle between existing currencies.
This is done by creating a CurerrencySwitcher.cshtml
partial with the following implementation:
@using Umbraco.Commerce.Core.Api;
@using Umbraco.Commerce.SwiftShop.Extensions;
@inject IUmbracoCommerceApi UmbracoCommerceApi
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
@{
var store = Model.GetStore();
var countries = await UmbracoCommerceApi.GetCountriesAsync(store.Id);
var currencies = await UmbracoCommerceApi.GetCurrenciesAsync(store.Id);
var currentCountry = await UmbracoCommerceApi.GetDefaultShippingCountryAsync(store.Id);
}
@if (countries.Count() > 1)
{
@using (Html.BeginUmbracoForm("ChangeCountry", "Culture", FormMethod.Post, new { @name = "changeCountryForm" }))
{
@Html.DropDownList("countryIsoCode", countries.Select(x
=> new SelectListItem(currencies.First(y => y.Id == x.DefaultCurrencyId!.Value).Code, x.Code, x.Code == currentCountry.Code)),
new
{
@class = "form-select form-select-sm",
@onchange = "document.forms['changeCountryForm'].submit()"
})
}
}
This can then be placed in your sites base template by adding the following:
@(await Html.PartialAsync("CurerrencySwitcher"))
Switching the culture is handled by a Surface controller.
Create a new Surface controller called CultureSurfaceController
and add the following code:
public class CultureSurfaceController : SurfaceController
{
private readonly IUmbracoCommerceApi _commerceApi;
public CultureSurfaceController(
IUmbracoContextAccessor umbracoContextAccessor,
IUmbracoDatabaseFactory databaseFactory,
ServiceContext services,
AppCaches appCaches,
IProfilingLogger profilingLogger,
IPublishedUrlProvider publishedUrlProvider,
IUmbracoCommerceApi commerceApi)
: base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
{
_commerceApi = commerceApi;
}
[HttpPost]
public async Task<IActionResult> ChangeCountry(ChangeCountryDto changeCountryDto)
{
var store = CurrentPage.GetStore();
var country = await _commerceApi.GetCountryAsync(store.Id, changeCountryDto.CountryIsoCode);
var currency = await _commerceApi.GetCurrencyAsync(country.DefaultCurrencyId.Value);
await _commerceApi.SetDefaultPaymentCountryAsync(store.Id, country);
await _commerceApi.SetDefaultShippingCountryAsync(store.Id, country);
await _commerceApi.SetDefaultCurrencyAsync(store.Id, currency);
var currentOrder = await _commerceApi.GetCurrentOrderAsync(store.Id);
if (currentOrder != null)
{
await _commerceApi.Uow.ExecuteAsync(async uow =>
{
var writableOrder = await currentOrder.AsWritableAsync(uow)
.ClearPaymentCountryRegionAsync()
.ClearShippingCountryRegionAsync()
.SetCurrencyAsync(currency.Id);
await _commerceApi.SaveOrderAsync(writableOrder);
uow.Complete();
});
}
return RedirectToCurrentUmbracoPage();
}
}
The ChangeCountryDto
class binds the country ISO code from the form.
public class ChangeCountryDto
{
public string CountryIsoCode { get; set; }
}
With the currency switcher implemented, users can switch between countries/currencies on your website.
The changes are reflected on the product details pages.