This article shows sample code for adding products to shopping cart
You can use sample code bellow since version 4.0:
// Get current shopping cart
ShoppingCartInfo cart = ECommerceContext.CurrentShoppingCart;
if (cart == null)
{
// If current shopping cart hasn't been created yet -> create new empty one
cart = ShoppingCartInfoProvider.CreateShoppingCartInfo(CMSContext.CurrentSite.SiteID);
// Associate new shopping cart with the current session
ECommerceContext.CurrentShoppingCart = cart;
}
int skuId = 158;
int quantity = 2;
int[] options = { 159, 160 }; //SkuIDs of product options
// Ensure shopping cart is saved to DB
if (cart.ShoppingCartID == 0)
{
ShoppingCartInfoProvider.SetShoppingCartInfo(cart);
}
// Add item to the shopping cart
ShoppingCartItemInfo product = cart.AddShoppingCartItem(skuId, quantity, options);
// Update shopping cart item in database
ShoppingCartItemInfoProvider.SetShoppingCartItemInfo(product);
// Update product options in database
foreach (ShoppingCartItemInfo option in product.ProductOptions)
{
ShoppingCartItemInfoProvider.SetShoppingCartItemInfo(option);
}
If you use some older version, please use following sample code:
// Get current shopping cart
ShoppingCartInfo cart = CMSContext.CurrentShoppingCart;
if (cart == null)
{
// If current shopping cart hasn't been created yet -> create new empty one
cart = ShoppingCartInfoProvider.CreateShoppingCartInfo(CMSContext.CurrentSite.SiteID);
// Associate new shopping cart with the current session
CMSContext.CurrentShoppingCart = cart;
}
int skuId = 10;
int quantity = 1;
// Ensure shopping cart is saved to DB
if (cart.ShoppingCartID == 0)
{
ShoppingCartInfoProvider.SetShoppingCartInfo(cart);
}
// Add product to shopping cart - you can call this method to add multiple products as you need
cart.AddSKUUnitsToShoppingCart(skuId, quantity);
// Save shopping cart products
ShoppingCartInfoProvider.SetShoppingCartItems(cart.ShoppingCartID, cart.CartItems);
See also:
Applies to:
Kentico CMS with E-commerce module
Created on
6/6/2009 6:53:18 AM in
E-commerce