Umbraco.Commerce.Issues icon indicating copy to clipboard operation
Umbraco.Commerce.Issues copied to clipboard

Using InitializeTransaction() and Finalize() results variationContext being reset to default

Open MichaelNielsenDK opened this issue 9 months ago • 0 comments

When doing something like this

var testVariationContextBefore = _variationContextAccessor.VariationContext?.Culture;

_umbracoCommerceApi.Uow.Execute(uow =>
{
var order = _umbracoCommerceApi.GetCurrentOrder(storeId)
						.AsWritable(uow);

//Finalize order
order.InitializeTransaction();
order.Finalize(order.TotalPrice, Guid.NewGuid().ToString("N"), PaymentStatus.Authorized);

_umbracoCommerceApi.SaveOrder(order);

    uow.Complete();
});

var testVariationContextAfter = _variationContextAccessor.VariationContext?.Culture;

And being on e.g. an english variant, the testVariationContextBefore variable will have a value of en, but testVariationContextAfter will have a value of da, as danish is the default variant.

Something is happening in InitializeTransaction() and/or Finalize() so variationContext is set to default.

If any redirection happen after finalizing, for example to a receipt page, the user will be redirected to the incorrect variant.

A temporary work around is fetch the variationContext prior to calling InitializeTransaction() and Finalize(), and then set it before redirections, so something like this

var currentVariationContext = _variationContextAccessor.VariationContext?.Culture;

_umbracoCommerceApi.Uow.Execute(uow =>
{
var order = _umbracoCommerceApi.GetCurrentOrder(storeId)
						.AsWritable(uow);

//Finalize order
order.InitializeTransaction();
order.Finalize(order.TotalPrice, Guid.NewGuid().ToString("N"), PaymentStatus.Authorized);

_umbracoCommerceApi.SaveOrder(order);

    uow.Complete();
});

_variationContextAccessor.VariationContext = new VariationContext(currentVariationContext);

return RedirectToUmbracoPage(receiptPage);

Umbraco 13.1.1 Umbraco Commerce 13.1.3

MichaelNielsenDK avatar May 08 '24 13:05 MichaelNielsenDK