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();
}
}