Take the community feedback survey now.

PuneetGarg
Dec 3, 2024
  62
(0 votes)

Shared optimizely cart between non-optimizley front end site

E-commerce ecosystems often demand a seamless shopping experience where users can shop across multiple sites using a single cart. Sharing a cart between an Optimizely site and a non-Optimizely site can be a challenge, but it's achievable with the right strategy. Here's a guide on how to set this up.

Overview

The goal is to enable users to share their cart and product information seamlessly across platforms. We achieve this by creating APIs that interact with the cart and product information. I've not added API's here.This implementation covers two scenarios:

  1. Logged-In Users
  2. Anonymous Users

1. Handling Logged-In Users

For logged-in users, leveraging their SSOID ensures that their context, including customer information and cart data, is consistent across sites. The SSOID is passed in the request header from the front end to the API.

 

Implementation Steps

  1. Capture SSOID from Header
    Use the Request.Headers to extract the userId (SSOID) in the backend.

    Request.Headers.TryGetValue("userId", out var ssoId);
    
  2. Fetch Customer Contact
    Using the SSOID, retrieve the logged-in user's context and cart data.
    private static CustomerContact GetCustomer(string ssoId)
    {
        var currentContact = CustomerContext.Current.CurrentContact;
        if (string.IsNullOrEmpty(ssoId))
            return currentContact;
    
        currentContact = CustomerContext.Current.GetContactByUserId($"String:{ssoId}");
        return currentContact;
    }
    
  3. Sync Cart DataWith the CustomerContact object, the cart data can be fetched and synced between the sites.

Key Notes

  • Ensure that the SSOID is securely transmitted in the header.
  • Proper error handling is crucial to avoid unexpected failures if the SSOID is missing or invalid.

 

2. Handling Anonymous Users

Anonymous users lack a consistent SSOID, but their session can still be tracked using a cookie (EPiServer_Commerce_AnonymousId).

Implementation Steps

  1. Capture Anonymous ID from Cookies
    Extract the cookie value that stores the anonymous user ID.

     Request.Cookies.TryGetValue("EPiServer_Commerce_AnonymousId", out var cookieValue); 
    Use Anonymous Cart
    With the cookieValue, fetch the anonymous user's cart from the backend.
  2. Share Cart Across Sites
    Sync the cart data between the sites using the retrieved anonymous user ID.

Key Notes

  • Cookies must be configured for cross-domain access if the sites are hosted on different domains.
  • Ensure proper handling of cookie expiration and invalidation scenarios.

 

API Design

Below is a high-level example of how the API endpoints could look:

Get Cart for Logged-In User

Endpoint: GET /api/cart/loggedin

Header:   userId: <SSOID>

Get Cart for Anonymous User

Endpoint: GET /api/cart/anonymous
Cookie:

EPiServer_Commerce_AnonymousId=<cookieValue>
 

Response Example

Both endpoints would return the cart in a standardized format, e.g.:

{
    "items": [
        {
            "productId": "12345",
            "name": "Product A",
            "quantity": 2,
            "price": 50.0
        },
        {
            "productId": "67890",
            "name": "Product B",
            "quantity": 1,
            "price": 25.0
        }
    ],
    "total": 125.0
}
 

Conclusion

By leveraging SSOID for logged-in users and cookies for anonymous users, we ensure a seamless shopping experience across sites. With a robust API that interacts with the cart and product information, users can enjoy a unified cart system, enhancing user satisfaction and driving conversions.

 

Note:- This is the work only when all sites share the same top main domain.

Dec 03, 2024

Comments

Please login to comment.
Latest blogs
A day in the life of an Optimizely OMVP - Opticon London 2025

This installment of a day in the life of an Optimizely OMVP gives an in-depth coverage of my trip down to London to attend Opticon London 2025 held...

Graham Carr | Oct 2, 2025

Optimizely Web Experimentation Using Real-Time Segments: A Step-by-Step Guide

  Introduction Personalization has become de facto standard for any digital channel to improve the user's engagement KPI’s.  Personalization uses...

Ratish | Oct 1, 2025 |

Trigger DXP Warmup Locally to Catch Bugs & Performance Issues Early

Here’s our documentation on warmup in DXP : 🔗 https://docs.developers.optimizely.com/digital-experience-platform/docs/warming-up-sites What I didn...

dada | Sep 29, 2025

Creating Opal Tools for Stott Robots Handler

This summer, the Netcel Development team and I took part in Optimizely’s Opal Hackathon. The challenge from Optimizely was to extend Opal’s abiliti...

Mark Stott | Sep 28, 2025

Integrating Commerce Search v3 (Vertex AI) with Optimizely Configured Commerce

Introduction This blog provides a technical guide for integrating Commerce Search v3, which leverages Google Cloud's Vertex AI Search, into an...

Vaibhav | Sep 27, 2025

A day in the life of an Optimizely MVP - Opti Graph Extensions add-on v1.0.0 released

I am pleased to announce that the official v1.0.0 of the Opti Graph Extensions add-on has now been released and is generally available. Refer to my...

Graham Carr | Sep 25, 2025