Shopping carts


List of examples:

Adding products to the current shopping cart




// Gets a product to add to the shopping cart
SKUInfo product = SKUInfoProvider.GetSKUs()
                                    .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);
}


> Back to list of examples

Adding products with product options to the current shopping cart




// Gets a product to add to the shopping cart
SKUInfo product = SKUInfoProvider.GetSKUs()
                                    .WhereEquals("SKUName", "NewProduct")
                                    .WhereNull("SKUOptionCategoryID")
                                    .TopN(1)
                                    .FirstOrDefault();

// Gets product options for the product
SKUInfo colorOption = SKUInfoProvider.GetSKUs()
                                           .WhereEquals("SKUName", "Red")
                                           .WhereNotNull("SKUOptionCategoryID")
                                           .TopN(1)
                                           .FirstOrDefault();
SKUInfo accessoryOption = SKUInfoProvider.GetSKUs()
                                            .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);
}


> Back to list of examples

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);
}


> Back to list of examples

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");


> Back to list of examples

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);
}


> Back to list of examples

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();


> Back to list of examples

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 = ShippingOptionInfoProvider.GetShippingOptions(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})"
    };
});


> Back to list of examples

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();


> Back to list of examples