Skip to content

Commerce Cart API Reference

advanced
📜Corecommerce

Cart operations use IOrderRepository and IOrderGroupFactory to create and manage shopping carts. Carts are named order groups stored per customer. The default cart name is "Default". Wishlist uses the name "Wishlist".

Optimizely.Commerce.Order

InterfacePurpose
IOrderRepositoryLoad, save, delete carts and orders
IOrderGroupFactoryCreate new cart and line item instances
IOrderGroupCalculatorCalculate cart totals
ILineItemValidatorValidate line items against inventory

Loads a cart by customer ID and cart name.

Parameters

NameTypeDescription
customerIdGuidCustomer contact GUID
namestringCart name (e.g., "Default", "Wishlist")

Returns: T where T is ICart, or null if no cart exists

Persists the cart to the database.

Parameters

NameTypeDescription
orderGroupIOrderGroupThe cart to save

Returns: OrderReference

Removes a cart from the database.

Parameters

NameTypeDescription
orderRefOrderReferenceReference returned from Save()

Returns: void

Create a new cart and add a line item
csharp
public class CartService
{
  private readonly IOrderRepository _orderRepository;
  private readonly IOrderGroupFactory _orderGroupFactory;
  private readonly ICurrentMarket _currentMarket;

  public CartService(
      IOrderRepository orderRepository,
      IOrderGroupFactory orderGroupFactory,
      ICurrentMarket currentMarket)
  {
      _orderRepository = orderRepository;
      _orderGroupFactory = orderGroupFactory;
      _currentMarket = currentMarket;
  }

  public ICart GetOrCreateCart(Guid customerId)
  {
      var cart = _orderRepository.Load<ICart>(customerId, "Default")
          .FirstOrDefault();

      if (cart == null)
      {
          cart = _orderGroupFactory.CreateCart(
              customerId,
              "Default",
              _currentMarket.GetCurrentMarket().MarketId);
      }

      return cart;
  }
}

Creates a new line item for a catalog entry code.

Parameters

NameTypeDescription
codestringCatalog entry code (SKU)
orderGroupIOrderGroupThe parent cart

Returns: ILineItem

Add item to cart
csharp
public void AddToCart(ICart cart, string skuCode, decimal quantity)
{
  var lineItem = cart.GetAllLineItems()
      .FirstOrDefault(li => li.Code == skuCode);

  if (lineItem != null)
  {
      lineItem.Quantity += quantity;
  }
  else
  {
      lineItem = _orderGroupFactory.CreateLineItem(skuCode, cart);
      lineItem.Quantity = quantity;
      cart.GetFirstShipment().LineItems.Add(lineItem);
  }

  _orderRepository.Save(cart);
}
Update line item quantity
csharp
public void UpdateQuantity(ICart cart, string skuCode, decimal newQuantity)
{
  var lineItem = cart.GetAllLineItems()
      .FirstOrDefault(li => li.Code == skuCode);

  if (lineItem == null)
      throw new InvalidOperationException($"Item {skuCode} not in cart.");

  if (newQuantity <= 0)
  {
      cart.GetFirstShipment().LineItems.Remove(lineItem);
  }
  else
  {
      lineItem.Quantity = newQuantity;
  }

  _orderRepository.Save(cart);
}
Remove item from cart
csharp
public void RemoveFromCart(ICart cart, string skuCode)
{
  var shipment = cart.GetFirstShipment();
  var lineItem = shipment.LineItems
      .FirstOrDefault(li => li.Code == skuCode);

  if (lineItem != null)
  {
      shipment.LineItems.Remove(lineItem);
      _orderRepository.Save(cart);
  }
}
PropertyTypeDescription
CodestringCatalog entry code
DisplayNamestringProduct display name
QuantitydecimalItem quantity
PlacedPricedecimalOriginal unit price
ListPricedecimalList price before discounts
DiscountedPricedecimalPrice after discounts
Cart namePurpose
"Default"Primary shopping cart
"Wishlist"Saved items for later
Custom stringAny named cart for specific workflows
ExceptionCause
InvalidOperationExceptionCart not found or line item missing
InventoryRequestExceptionRequested quantity not available
ValidationExceptionInvalid entry code or negative quantity