You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 15/umbraco-commerce/tutorials/build-a-store/custom-cart.md
+31-26Lines changed: 31 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,11 +4,11 @@ description: Learn how to build a custom shopping cart in Umbraco Commerce.
4
4
5
5
# Creating a Custom Shopping Cart
6
6
7
-
If you need a more custom shopping cart experience, you can build your own 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 specific requirements.
7
+
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.
8
8
9
9
## Create a Cart Surface Controller
10
10
11
-
Before we add any functionality to our custom shopping cart, we need to create a Surface Controller to handle the cart actions.
11
+
Before adding any functionality to the custom shopping cart, you must create a Surface Controller to handle the cart actions.
12
12
13
13
1. Create a new class in your project and inherit from `Umbraco.Cms.Web.Website.Controllers.SurfaceController`.
14
14
2. Name the class `CartSurfaceController`.
@@ -49,9 +49,9 @@ public class CheckoutSurfaceController : SurfaceController
49
49
50
50
## Adding a Product to the Cart
51
51
52
-
To add a product to the cart, we need to create an action method in our`CartSurfaceController` that accepts a `productReference` or `productVariantReference` and adds it to the cart. We'll wrap these properties in a DTO class to pass it to the controller.
52
+
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.
53
53
54
-
1. Create a new class in your project named `AddToCartDto`with the following properties.
54
+
1. Create a new class named `AddToCartDto`using the following properties.
55
55
56
56
```csharp
57
57
namespaceUmbraco.Commerce.DemoStore.Dtos;
@@ -63,6 +63,7 @@ public class AddToCartDto
63
63
publicdecimalQuantity { get; set; } =1;
64
64
}
65
65
```
66
+
66
67
2. Add the following action method to your `CartSurfaceController`:
67
68
68
69
```csharp
@@ -93,7 +94,9 @@ public async Task<IActionResult> AddToCart(AddToCartDto postModel)
93
94
returnRedirectToCurrentUmbracoPage();
94
95
}
95
96
```
96
-
3. In the view where you want to add a product to the cart, create a form that posts to the `AddToCart` action of your `CartSurfaceController`.
97
+
98
+
3. Open the view where you want to add a product to the cart.
99
+
4. Create a form that posts to the `AddToCart` action of your `CartSurfaceController`.
@@ -112,15 +115,15 @@ public async Task<IActionResult> AddToCart(AddToCartDto postModel)
112
115
</div>
113
116
```
114
117
115
-
4.On the front end, when the user clicks the "Add to Basket" button, the product will be added to the cart.
118
+
On the front end, when the user clicks the "Add to Basket" button, the product will be added to the cart.
116
119
117
120

118
121
119
122
### Showing a Cart Count
120
123
121
-
Total cart quantity is managed through a [view component](https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-9.0) that displays a simple counter near the shopping cart icon.
124
+
The total cart quantity is managed through a [view component](https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-9.0) that displays a counter near the shopping cart icon.
122
125
123
-
1. Create a new view component in your project named `CartCountViewComponent`.
126
+
1. Create a new view component named `CartCountViewComponent`.
124
127
125
128
````csharp
126
129
[ViewComponent]
@@ -151,7 +154,7 @@ public class CartCountViewComponent : ViewComponent
151
154
152
155
### Showing Cart Notifications
153
156
154
-
1. Create a new class in your project named `NotificationModel`.
157
+
1. Create a new class named `NotificationModel`.
155
158
156
159
```csharp
157
160
publicclassNotificationModel
@@ -167,9 +170,9 @@ public enum NotificationType
167
170
}
168
171
```
169
172
170
-
2. Create a partial view named `Notification.cshtml`
173
+
2. Create a partial view named `Notification.cshtml`.
2. Reference the partial view in your layout or view to display cart notifications.
194
+
3. Reference the partial view in your layout or view to display cart notifications.
193
195
194
196
```csharp
195
197
@{
@@ -209,10 +211,10 @@ public enum NotificationType
209
211
210
212
## Displaying the Cart
211
213
212
-
To display the cart, you can create a new view that lists the items in the cart and allows the user to update or remove items.
214
+
You can create a new view that lists the items in the cart and allows the user to update or remove items.
213
215
214
-
1. Create a new `Cart`document type and add it as a content node beneath the store root.
215
-
7. Open the `Cart.cshtml` template created by your document type and add the following.
216
+
1. Create a new `Cart`Document Type and add it as a content node beneath the store root.
217
+
7. Open the `Cart.cshtml` template created by your Document Type and add the following.
216
218
217
219
```csharp
218
220
@{
@@ -278,16 +280,17 @@ To display the cart, you can create a new view that lists the items in the cart
278
280
}
279
281
}
280
282
```
281
-
3. On the front end, navigate to the cart page to view the items in the cart.
282
283
283
-

284
+
3. Access the frontend of the website.
285
+
4. Navigate to the cart page to view the items in the cart.
284
286
287
+

285
288
286
289
## Updating a Cart Item
287
290
288
-
In our[Cart view above](#displaying-the-cart)we have wrapped our cart markup in a form that posts to an `UpdateCart` action on our`CartSurfaceController`. Additionally, for each order line we 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.
291
+
In the[Cart view above](#displaying-the-cart)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.
289
292
290
-
1. Create a new class in your project named `UpdateCartDto`with the following properties.
293
+
1. Create a new class named `UpdateCartDto`using the following properties.
291
294
292
295
```csharp
293
296
namespaceUmbraco.Commerce.DemoStore.Dtos;
@@ -343,15 +346,16 @@ public async Task<IActionResult> UpdateCart(UpdateCartDto postModel)
343
346
}
344
347
```
345
348
346
-
3. On the front end, update the quantity of an item in the cart and click the **Update Cart** button to update the cart.
349
+
3. Access the frontend of the website.
350
+
4. Update the quantity of an item in the cart and click the **Update Cart** button.
347
351
348
352
## Removing a Cart Item
349
353
350
-
In our[Cart view above](#displaying-the-cart) for each order line we render a remove link that triggers a `RemoveFromCart` action on our`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.
354
+
In the[Cart view above](#displaying-the-cart) 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.
351
355
352
356
To hook up the remove link, perform the following steps:
353
357
354
-
1. Create a new class in your project named `RemoveFromCartDto`with the following properties.
358
+
1. Create a new class named `RemoveFromCartDto`using the following properties.
355
359
356
360
```csharp
357
361
namespaceUmbraco.Commerce.DemoStore.Dtos;
@@ -391,11 +395,12 @@ public async Task<IActionResult> RemoveFromCart(RemoveFromCartDto postModel)
391
395
}
392
396
```
393
397
394
-
3. On the front end, click the **Remove** link on the cart item to remove it from the cart.
398
+
3. Access the frontend of the website.
399
+
4. Click the **Remove** link on the cart item to remove it from the cart.
395
400
396
401
## Useful Extension Methods
397
402
398
-
In the above examples, we used a number of `IPublishedContent` extension methods to simplify our code. Here are some of the most useful extension methods:
403
+
In the examples above, you used several `IPublishedContent` extension methods to simplify the code. Here are some of the most useful extension methods:
0 commit comments