Shopping carts


List of examples:

Adding products to a shopping cart




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

if (product != null)
{
    // Gets the current shopping cart
    ShoppingCartInfo cart = ECommerceContext.CurrentShoppingCart;

    // Creates the shopping cart in the database if necessary
    ShoppingCartInfoProvider.SetShoppingCartInfo(cart);

    // Prepares a shopping cart item representing 1 unit of the product
    ShoppingCartItemParameters parameters = new ShoppingCartItemParameters(product.SKUID, 1);
    ShoppingCartItemInfo cartItem = cart.SetShoppingCartItem(parameters);

    // Saves the shopping cart item to the shopping cart
    ShoppingCartItemInfoProvider.SetShoppingCartItemInfo(cartItem);

    // Invalidates the shopping cart's calculated values (to trigger recalculation of discounts, shipping, price totals, etc).
    cart.InvalidateCalculations();
}


> Back to list of examples

Updating the number of units of a shopping cart item




// Gets the product
SKUInfo product = SKUInfoProvider.GetSKUs()
                                    .WhereEquals("SKUName", "NewProduct")
                                    .WhereNull("SKUOptionCategoryID")
                                    .FirstObject;

if (product != null)
{
    // Prepares the shopping cart item
    ShoppingCartItemInfo item = null;

    // Gets the current shopping cart
    ShoppingCartInfo cart = ECommerceContext.CurrentShoppingCart;

    // Loops through the items in the shopping cart
    foreach (ShoppingCartItemInfo cartItem in cart.CartItems)
    {
        // Gets the first shopping cart item matching the specified product
        if (cartItem.SKUID == product.SKUID)
        {
            item = cartItem;
            break;
        }
    }

    if (item != null)
    {
        // Updates the unit count of the shopping cart item to 3
        ShoppingCartItemInfoProvider.UpdateShoppingCartItemUnits(item, 3);

        // Invalidates the shopping cart's calculated values (to trigger recalculation of discounts, shipping, price totals, etc).
        cart.InvalidateCalculations();
    }
}


> Back to list of examples

Removing products from a shopping cart




// Gets the current shopping cart
ShoppingCartInfo cart = ECommerceContext.CurrentShoppingCart;

// Gets the product
SKUInfo product = SKUInfoProvider.GetSKUs()
                                    .WhereEquals("SKUName", "NewProduct")
                                    .WhereNull("SKUOptionCategoryID")
                                    .FirstObject;

if ((cart != null) && (product != null))
{
    // Gets the first item matching the product in the current shopping cart
    ShoppingCartItemInfo item = ShoppingCartItemInfoProvider.GetShoppingCartItems()
                                            .WhereEquals("SKUID", product.SKUID)
                                            .WhereEquals("ShoppingCartID", cart.ShoppingCartID)
                                            .FirstObject;

    if (item != null)
    {
        // Removes the item from the shopping cart
        cart.RemoveShoppingCartItem(item.CartItemID);

        // Removes the item from the database
        ShoppingCartItemInfoProvider.DeleteShoppingCartItemInfo(item);

        // Invalidates the shopping cart's calculated values (to trigger recalculation of discounts, shipping, price totals, etc).
        cart.InvalidateCalculations();
    }
}


> Back to list of examples