How to set country on cart

We have a litium 7 accelerator with multiple channel on same domain we also want to use klarna checkout v3 but we have a problem that the cart is shared and not updated when switching channel.

Since the channels all share the same domain you essentially share the same cart and this is a problem not only if you have channel specific product in this current case is a problem with klarna checkout since country is set on the cart when first created. This results in klarna thinking that the address need to be in that country. Doesnt matter what channel you visit after you visited the first one klarna says the address need to be in that country.

How and where should one set country on the cart to make the accelerator play nice with klarna checkout in our scenario? from what is sent to klarna it looks like it is billing- or delivery address that has a country setting that control this when loadning the klarna widget.

customer.se/se
customer.se/fi
customer.se/dk

Litium version: 7.3

I think you can use the same code that was added in the ControllerBase.Initialize-method inside the Litium.Accelerator.Mvc-package to ensure that the correct channel (and other information) is set up on each request.

Download the latest accelerator package to find the code.

I found the set channel but it doesnt look lik it is enough so i will look at the accelerator code.

protected override void Initialize(RequestContext requestContext)
{
base.Initialize(requestContext);
var cart = CartAccessor.Cart;
if (cart.HasOrderCarrier)
{
var routeRequestLookupInto = RouteRequestLookupInfoAccessor.RouteRequestLookupInfo;

            var channel = routeRequestLookupInto.Channel;
            if (cart.OrderCarrier.ChannelID != channel.SystemId)
            {
                cart.SetChannel(
                    channel,
                    channel.CountryLinks.FirstOrDefault()?.CountrySystemId.MapTo<Country>(),
                    SecurityToken.CurrentSecurityToken);
            }
        }
    }

There was a little differance in the new version but it does not work since we have a cart with a country and instead of updating country the 7.4 doesnt even try to change country if it is already set. But setting country isnt enough anyway.

if (cart.OrderCarrier.ChannelID != channel.SystemId)
            {
                var countryId = cart.OrderCarrier?.CountryID;
                //if cart have no country then using the first country link of request lookup channel
                if (countryId == null || countryId == Guid.Empty)
                {
                    countryId = channel.CountryLinks.FirstOrDefault()?.CountrySystemId;
                }

                cart.SetChannel(
                    channel,
                    countryId.MapTo<Country>(),
                    SecurityToken.CurrentSecurityToken);
            }

adding cart.Clear(); if channel changed to the code we had before looks like it solves the issues we had.

protected override void Initialize(RequestContext requestContext)
    {
        base.Initialize(requestContext);
        var cart = CartAccessor.Cart;
        if (cart.HasOrderCarrier)
        {
            
            var routeRequestLookupInto = RouteRequestLookupInfoAccessor.RouteRequestLookupInfo;

            var channel = routeRequestLookupInto.Channel;
            if (cart.OrderCarrier.ChannelID != channel.SystemId)
            {
                cart.Clear();
                cart.SetChannel(
                    channel,
                    channel.CountryLinks.FirstOrDefault()?.CountrySystemId.MapTo<Country>(),
                    SecurityToken.CurrentSecurityToken);
            }
        }
    }

With use or the cart.Clear(); you will remove all the items that already have been placed in the cart also, and that may not be what the buyer expect.

I haven’t tested but it can be that the information is sent to Klarna during the first load of the checkout page and not updated in the next one even that the information is changed on the cart.

I see the downside of Cart.Clear() but it is the only way right now that i have see to get klarna checkout working at all for us.

Now i just need to find out why we are always redirected to the swedish order confirmation.

I think that is a part that also is changed in later version of the accelerator and the OrderCarrier.ChannelID is used instead of the channel for the current request/domain name.

Yes i saw that the GetOrderConfirmation method in the accelerator used request channelId instead of OrderCarrier.ChannelId. I guess this is due to that the klarna redirect url is always customer.se/site.axd regardless of channel order is placed on.

Yes, that’s the reason.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.