Cart Management from frontend
Some solutions – like CPQ (Configure, Price, Quote) solutions – need to be implemented in a way which allows users to work with carts from frontend, often multiple named carts.
In this guide we will implement a simple setup where the customer can work with carts from frontend – in concrete terms:
- Show a list of carts to a user
- Create new empty carts
- Switch between open carts
- Change cart states from frontend
We will also touch upon how users with impersonation rights can apply custom discounts to open carts.
Most of the functionality implemented in this guide uses cart commands – and while cart commands can be used anywhere, some of the more advanced functionalities depend on the customer center app. There are two ways to tackle this:
- Implement this funcitonality inside a customer center template
- Implement this functionality using the more modern customer experience center, and use javascript to post to a customer center elsewhere on the solution when required
In this guide we will go for #2, as the Customer Experience Center is much nicer to work with than the old customer center.
Part 1: Creating the Cart list
The first thing you want to do is show a list of open carts to a user:
- Add a Customer Experience Center app to a paragraph
- Under Order type select Carts
- Under Retrieve list based on select Own orders and orders made by users that current user can impersonate
- Under List select a field to sort by – e.g. ID – and an appropriate sort order
- Under Templates select or create a simple template for rendering the list of carts associated with the user – it could look like this:
Part 2: Showing the current cart details
Now that we have the list of carts associated with the user and any users that the current user can impersonate we want to see more details about the currently active cart.
This can be done very easily by fetching the cart object using the API and rendering details about it – here in a sort of cart-like view:
Part 3: Renaming, copying & archiving carts
Now that you can see information about the current cart we can implement some of the common cart management operations:
- (Re)naming
- Copying
- Archiving
This is, as mentioned previously, done by using cart management cart commands – specifically setname, copy, and setname. In this example we’ve created two buttons for copying & archiving a cart, and a form for setting the display name to something else:
Part 4: Setting the active cart
Now that you can do things to the active cart, it’s time to make it possible to set the active cart. This is done using the setcart command with a cartid parameter – here implemented in the list of carts.
Part 5: Creating new carts
Normally, carts are created when there is no active cart and a customer adds a product to cart – but you can also create new empty carts from frontend using the createnew cart command. This cart command takes two optional parameters; a CartName property and a CartUserId property. The CartUserId property must be set to either the id of the current user or a user he or she can impersonate.
Here we’ve added a simple form for creating a named cart associated with the current user – in more advanced setups you could make it possible to select a user to impersonate and create carts on their behalf.
Part 6: Changing states from frontend
On some solutions you may want to use cart states to create a sort of workflow for carts to move through before being checked out. Currently this feature depends on submitting a form with specific parameters to a customer center app. As mentioned in the introduction to this guide, we’ve chosen to implement this by using javascript to post a form to a customer center app elsewhere on the solution. You can also choose to implement the whole cart management feature in a customer center app template – that’s up to you.
In this example we do the following:
- Use the API to fetch cart states based on a property on the cart object
- Use a bit of JS to submit form data to a page with a customer center app but prevent redirect and instead reload the current page
- Render a form which makes it possible to change the cart state by posting to the page with the customer center app (ID=2) with a parameter required by the customer center (CC1=Carts)
Part 7: Custom discounts
The final thing we will add to this guide is how to apply custom discounts from frontend. This is typically implemented when you have a solution with key account managers or sales people who also interact with orders from frontend – by creating, receiving and/or processing carts from customers.
To implement this you should:
- Create discounts of the type custom amount discount and custom percentage discount
- Log in as a user with impersonation rights
You can then use the setdiscount cart command to assign either a percentage based or a fixed amount based discount to a cart, depending on the parameter used: