Implement the checkout process

Advanced license required

Features described on this page require the Xperience by Kentico Advanced license tier.

Developer preview feature

The digital commerce feature is currently not fully functional, and primarily intended to allow technical users to familiarize themselves with the development process of the commerce feature. You can expect the feature to be updated and extended in upcoming releases.

The related API is marked as experimental and usage will result in warnings when compiling your project. The warnings are treated as errors for reporting purposes. To use the code, you need to suppress the warnings.

What should you do with this feature?

  • DO try out development of digital commerce and examine the sample in the Dancing Goat project template.
  • DO feel free to share your feedback with the Kentico Product team.
  • DO NOT use this feature in public facing and production applications, as the feature is currently incomplete.

Most of the checkout process for needs to be implemented by developers. This is because checkout requirements (e.g., payment methods, shipping options, tax handling, order confirmation) can vary greatly between projects. The commerce feature provides the core building blocks, but it’s up to the development team to customize the checkout process to match the specific needs of the shop.

To implement a checkout process in your commerce solution, you need to handle the following steps:

  1. Store products in the shopping cart.
  2. Retrieve the current shopping cart.
  3. Create a new order from the shopping cart.

Commerce object types

Object types used by the commerce feature (Shopping cart, Order, Order address, Order item, Customer, Customer address …):

  • are considered live-site data and are not supported by the CI/CD feature
  • can be extended by adding custom fields

Implement the shopping cart

Shopping carts are stored as ShoppingCartInfo objects in the API. The actual content of a shopping cart is stored in the ShoppingCartData string property. It is up to you to implement the data model of the shopping cart and methods to serialize and deserialize the shopping cart content.

For a sample implementation, you can check the Dancing Goat project template, which uses a ShoppingCartDataModel object to represent the data model of the shopping cart. The model contains a collection of ShoppingCartDataItem objects representing individual items in the shopping cart. You can use the GetShoppingCartDataModel and StoreShoppingCartDataModel sample extension methods of the ShoppingCartInfo object to modify what products and quantities are in the shopping cart.

C#
Sample shopping cart handling in the Dancing Goat project template

// ID of the content item representing the product
int contentItemId;
// ID of the product variant
int variantId;
// New quantity of the product
int quantity;

// Retrieves the shopping cart for the current user
ShoppingCartInfo shoppingCart = await currentShoppingCartService.Get();

// Retrieves a custom data model of the shopping cart
ShoppingCartDataModel shoppingCartData = shoppingCart.GetShoppingCartDataModel();

// Retrieves a custom product model from the data model
ShoppingCartDataItem productItem = shoppingCartData.Items.FirstOrDefault(
                    x => x.ContentItemId == contentItemId && x.VariantId == variantId);

// Checks that the product is already in the shopping cart
if (productItem != null)
{
    // Sets the new quantity of the product
    productItem.Quantity = quantity;
    // If the new quantity is 0, removes the product from the cart
    if (productItem.Quantity == 0)
    {
        shoppingCartData.Items.Remove(productItem);
    }
}
// Checks that the product is not in the shopping cart and the new quantity is more than 0
else if (quantity > 0)
{
    // Adds a new product to the shopping cart
    shoppingCartData.Items.Add(new ShoppingCartDataItem { ContentItemId = contentItemId, Quantity = quantity, VariantId = variantId });
}

// Stores the new data model to the shopping cart of the current user
shoppingCart.StoreShoppingCartDataModel(shoppingCartData);

Retrieve the current shopping cart

Current shopping cart can be retrieved using the ICurrentShoppingCartService service (available in the Kentico.Commerce.Web.Mvc namespace). The shopping cart identifier is stored in a cookie for an anonymous customer. For a member (a user signed in on the website), the shopping cart contains a reference to the respective member, so that the content of the shopping cart is consistent across multiple devices. The actual shopping cart data is stored in the database. This allows members to access their cart even if they sign in from a different device – their cart will be correctly populated based on their previous actions. You can also configure the amount of time after which a shopping cart is deleted.

Use the following methods of the interface to work with the shopping cart of the current user:

  • Get – retrieves a ShoppingCartInfo object representing the current shopping cart.
  • Create – creates a new ShoppingCartInfo object and stores the identifier as a cookie.
  • Delete – deletes the shopping cart from the database and removes the identifier from the cookie.
  • MemberSignIn – handles the transition from anonymous user to member during sign-in. If the member already has a cart and there is also an anonymous cart from the current session, the existing member cart is deleted and the anonymous cart is assigned to the signed-in member. This behavior can be customized by overriding the service.
C#
Current shopping cart

// An instance of ICurrentShoppingCartService (e.g., obtained via dependency injection)
private readonly ICurrentShoppingCartService currentShoppingCartService;

// Retrieves the shopping cart for the current user
ShoppingCartInfo shoppingCart = await currentShoppingCartService.Get();

// If the there is no shopping cart for the current user, a new shopping cart is created
shoppingCart ??= await currentShoppingCartService.Create(null);

// Deletes the shopping cart for the current user
await currentShoppingCartService.Delete();

Implement order creation

Orders are represented as OrderInfo objects in the API. To create a new order from a shopping cart, you need to:

  1. Retrieve a list of products from the shopping cart.
  2. Calculate the total price of the order.
  3. Create a new CustomerInfo object if the customer is anonymous or use an existing customer object if it exists already.
    • Use the CustomerAddressInfo object to represent the customer address. This address can be used to pre-fill the shipping/billing address.
  4. Create the OrderInfo object.
    • Use the OrderAddressInfo object to represent addresses related to of the order. Use the OrderAddressType to specify whether the address is billing or shipping. You can attach more than 1 address to 1 order.
    • Use the OrderItemInfo object to represent individual product items in the order.

After you create the order, you can see the new order in the Orders application.