Creating a Custom Shopping Cart
Learn how to build a custom shopping cart in Umbraco Commerce.
If you need a more custom shopping cart experience, you can build a custom shopping cart using the Umbraco Commerce API. This approach gives you full control over the shopping cart experience, allowing you to tailor it to your requirements.
Create a Cart Surface Controller
Before adding any functionality to the custom shopping cart, you must create a Surface Controller to handle the cart actions.
Create a new class in your project and inherit from
Umbraco.Cms.Web.Website.Controllers.SurfaceController.Name the class
CartSurfaceController.Add the following code to the class:
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Web.Website.Controllers;
using Umbraco.Commerce.Core.Api;
namespace Umbraco.Commerce.DemoStore.Controllers;
public class CheckoutSurfaceController : SurfaceController
{
private readonly IUmbracoCommerceApi _commerceApi;
public CheckoutSurfaceController(
IUmbracoContextAccessor umbracoContextAccessor,
IUmbracoDatabaseFactory databaseFactory,
ServiceContext services,
AppCaches appCaches,
IProfilingLogger profilingLogger,
IPublishedUrlProvider publishedUrlProvider,
IUmbracoCommerceApi commerceApi)
: base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
{
_commerceApi = commerceApi;
}
// Add your cart actions here
}Adding a Product to the Cart
To add a product to the cart, create an action method in the CartSurfaceController. The action should accept a productReference or productVariantReference and add the product to the cart. The properties will be wrapped in a DTO class to pass on to the controller.
Create a new class named
AddToCartDtousing the following properties.
Add the following action method to your
CartSurfaceController.
Open the view where you want to add a product to the cart.
Create a form that posts to the
AddToCartaction of yourCartSurfaceController.
On the front end, when the user clicks the "Add to Basket" button, the product will be added to the cart.

Showing a Cart Count
The total cart quantity is managed through a view component that displays a counter near the shopping cart icon.
Create a new view component named
CartCountViewComponent.
Showing Cart Notifications
Create a new class named
NotificationModel.
Create a partial view named
Notification.cshtml.
Reference the partial view in your layout or view to display cart notifications.
Displaying the Cart
You can create a new view that lists the items in the cart and allows the user to update or remove items.
Create a new
CartDocument Type and add it as a content node beneath the store root.Open the
Cart.cshtmltemplate created by your Document Type and add the following.
Access the frontend of the website.
Navigate to the cart page to view the items in the cart.

Updating a Cart Item
In the Cart view above you have wrapped the cart markup in a form that posts to an UpdateCart action on the CartSurfaceController. Additionally, for each order line, you render a hidden input for the Id of the order line and a numeric input for it's Quantity. When the Update Cart button is clicked, the form will post all the order lines and their quantities to the UpdateCart action to be updated.
Create a new class named
UpdateCartDtousing the following properties.
Add the following action method to your
CartSurfaceController:
Access the frontend of the website.
Update the quantity of an item in the cart and click the Update Cart button.
Removing a Cart Item
In the Cart view above for each order line you render a remove link that triggers a RemoveFromCart action on the CartSurfaceController. This uses the Url.SurfaceAction helper to call a surface action as a GET request instead of a POST request. When the Remove link is clicked, the order line will be removed from the cart.
To hook up the remove link, perform the following steps:
Create a new class named
RemoveFromCartDtousing the following properties.
Add the following action method to your
CartSurfaceController:
Access the frontend of the website.
Click the Remove link on the cart item to remove it from the cart.
Useful Extension Methods
In the examples above, you used several IPublishedContent extension methods to simplify the code. Here are some of the most useful extension methods:
Last updated
Was this helpful?