Shopping carts
List of examples:
- Adding products to the current shopping cart
- Adding products with product options to the current shopping cart
- Updating the unit count of a shopping cart item
- Adding coupon codes to the current shopping cart
- Removing a product from the current shopping cart
- Removing all products from the current shopping cart
- Calculating the shipping price
- Creating a new order from the current shopping cart
Adding products to the current shopping cart
// Gets a product to add to the shopping cart
SKUInfo product = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "NewProduct")
.WhereNull("SKUOptionCategoryID")
.TopN(1)
.FirstOrDefault();
if (product != null)
{
// Gets an instance of the shopping service used to manage the current shopping cart
IShoppingService shoppingService = Service.Resolve<IShoppingService>();
// Adds 1 unit of the product to the current shopping cart
shoppingService.AddItemToCart(product.SKUID, 1);
}
Adding products with product options to the current shopping cart
// Gets a product to add to the shopping cart
SKUInfo product = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "NewProduct")
.WhereNull("SKUOptionCategoryID")
.TopN(1)
.FirstOrDefault();
// Gets product options for the product
SKUInfo colorOption = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "Red")
.WhereNotNull("SKUOptionCategoryID")
.TopN(1)
.FirstOrDefault();
SKUInfo accessoryOption = SKUInfo.Provider.Get()
.WhereEquals("SKUName", "AccessoryProduct")
.WhereNotNull("SKUOptionCategoryID")
.TopN(1)
.FirstOrDefault();
if (product != null && colorOption != null && accessoryOption != null)
{
// Prepares a ShoppingCartItemParameters object representing 1 unit of the product,
// with the specified product options
var optionIds = new List<int>
{
colorOption.SKUID,
accessoryOption.SKUID
};
var cartItemParams = new ShoppingCartItemParameters(product.SKUID, 1, optionIds);
// Gets an instance of the shopping service used to manage the current shopping cart
IShoppingService shoppingService = Service.Resolve<IShoppingService>();
// Adds the product to the current shopping cart based on the ShoppingCartItemParameters
shoppingService.AddItemToCart(cartItemParams);
}
Updating the unit count of a shopping cart item
// Gets an instance of the shopping service used to manage the current shopping cart
IShoppingService shoppingService = Service.Resolve<IShoppingService>();
// Gets the first product from the current shopping cart
ShoppingCartItemInfo product = shoppingService
.GetCurrentShoppingCart()
.CartProducts
.FirstOrDefault();
if (product != null)
{
// Updates the quantity of the shopping cart product to 3
shoppingService.UpdateItemQuantity(product.CartItemID, 3);
}
Adding coupon codes to the current shopping cart
// Gets an instance of the shopping service used to manage the current shopping cart
IShoppingService shoppingService = Service.Resolve<IShoppingService>();
// Adds the 'SAVE10' coupon code to the cart, if it is related to a valid discount for the current shopping cart content
// Automatically evaluates and recalculates the shopping cart
shoppingService.AddCouponCode("SAVE10");
Removing a product from the current shopping cart
// Gets an instance of the shopping service used to manage the current shopping cart
IShoppingService shoppingService = Service.Resolve<IShoppingService>();
// Gets the first product from the current shopping cart
ShoppingCartItemInfo product = shoppingService.GetCurrentShoppingCart()
.CartProducts
.FirstOrDefault();
if (product != null)
{
// Removes a product from the current shopping cart
shoppingService.RemoveItemFromCart(product.CartItemID);
}
Removing all products from the current shopping cart
// Gets an instance of the shopping service used to manage the current shopping cart
IShoppingService shoppingService = Service.Resolve<IShoppingService>();
// Removes all products from the current shopping cart
shoppingService.RemoveAllItemsFromCart();
Calculating the shipping price
// Gets an instance of the shopping service used to manage the current shopping cart
IShoppingService shoppingService = Service.Resolve<IShoppingService>();
// Gets a collection of shipping options configured and enabled for the current site
// Shipping options can be configured on the 'Shipping options' tab
// of the 'Store configuration' or 'Multistore configuration' application
IEnumerable<ShippingOptionInfo> shippingOptionInfos = ShippingOptionInfo.Provider.GetBySite(SiteContext.CurrentSiteID, true);
// Calculates the shipping price for the given shipping options
// based on the contents of the current shopping cart and
// currently running store promotions (e.g., free shipping offers)
IEnumerable<object> shippingPrices = shippingOptionInfos.Select(shippingOption =>
{
decimal shippingPrice = shoppingService.CalculateShippingOptionPrice(shippingOption);
return new
{
id = shippingOption.ShippingOptionID,
name = $"{shippingOption.ShippingOptionDisplayName} ({shippingPrice})"
};
});
Creating a new order from the current shopping cart
// Gets an instance of the shopping service used to manage shopping cart instances
IShoppingService shoppingService = Service.Resolve<IShoppingService>();
// Creates a new order based on the contents of the current shopping cart
// Deletes the shopping cart after the order is created
shoppingService.CreateOrder();