Try our conversational search powered by Generative AI!

PuneetGarg
Jan 16, 2024
  253
(0 votes)

Retain Cart when market is change

A cart is a collection of products that are selected by a website customer.

Working on multi-market site, comes with a problem when we switch from one market to another market it does create new cart for each market.

Note from optimizely :- Carts are market-dependent, so are unique to a user and market, instead of just a user. If a site lets a user switch markets, then switching the market may also switch associate carts to a new or different carts.

So, here is my solution for this instead of recreating the whole cart, we can just change the market fields and payment data in cart and market data in cookie(i.e for users).

Step-1 

Delete if any existing carts are present to your desitination market.

 var userId = _customerContext.CurrentContactId.ToString();
 var cartName = cart.Name;

var previousCart = _orderRepository.LoadCart<ICart>(new Guid(userId), cartName, selectedShippingAddressRegion);

if (previousCart != null)
    _orderRepository.Delete(previousCart.OrderLink);

 

Step-2

Change Market data in current cart

cart.MarketId = new MarketId(destinationMarket);
var marketName = _marketManager.GetAllActiveMarkets().Where(c => c.IsEnabled)
                .FirstOrDefault(x => x.MarketId.Value == destinationMarket)?.MarketName;

cart.MarketName = marketName;
this._orderRepository.Save(cart);

 

Step-3 

Update cookie with destinatin market

 var market = GetMarket(marketId);
 SiteContext.Current.Currency = market.DefaultCurrency;
 _cookieService.Set(MarketCookie, marketId.Value);
 MarketEvent.OnChangeMarket(market, new EventArgs());

Step-4 

Updated cart amount(price) with new price from commerce manager 
if (cart != null)
{
     UpdateLineItemPrice(cart);
     _orderRepository.Save(cart);
}

 public void UpdateLineItemPrice(ICart cart)
 {
    if (cart == null)
        return;
     foreach (var lineItem in cart.GetAllLineItems())
      {
        lineItem.PlacedPrice = _priceCalculationManager.GetSalePrice(lineItem.Code, cart.MarketId, cart.Currency).UnitPrice.Amount;
       }
        ValidateCart(cart);
}

Result:- With these steps when we change the market it will carry our current cart to new market with updated market fields and price.

Notes :- 

  • All service call made with dependency injection and all of them are present with optimizely liberary.
  • Depending with impemention of cart , we might need to update shipping address as well it will be similar like what we did with price.
  • Price should be present at the variant level in commerce manager with respect to market.
Jan 16, 2024

Comments

Please login to comment.
Latest blogs
A day in the life of an Optimizely Developer - Enabling Opti ID within your application

Hello and welcome to another instalment of A Day In The Life Of An Optimizely developer, in this blog post I will provide details on Optimizely's...

Graham Carr | May 9, 2024

How to add a custom property in Optimizely Graph

In the Optimizely CMS content can be synchronized to the Optimizely Graph service for it then to be exposed by the GraphQL API. In some cases, you...

Ynze | May 9, 2024 | Syndicated blog

New Security Improvement released for Optimizely CMS 11

A new security improvement has been released for Optimizely CMS 11. You should update now!

Tomas Hensrud Gulla | May 7, 2024 | Syndicated blog

Azure AI Language – Key Phrase Extraction in Optimizely CMS

In this article, I demonstrate how the key phrase extraction feature, offered by the Azure AI Language service, can be used to generate a list of k...

Anil Patel | May 7, 2024 | Syndicated blog