Skip to content

Commit 9ea568a

Browse files
authored
Update custom-cart.md
1 parent 4eb8def commit 9ea568a

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

15/umbraco-commerce/tutorials/build-a-store/custom-cart.md

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ description: Learn how to build a custom shopping cart in Umbraco Commerce.
44

55
# Creating a Custom Shopping Cart
66

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.
88

99
## Create a Cart Surface Controller
1010

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.
1212

1313
1. Create a new class in your project and inherit from `Umbraco.Cms.Web.Website.Controllers.SurfaceController`.
1414
2. Name the class `CartSurfaceController`.
@@ -49,9 +49,9 @@ public class CheckoutSurfaceController : SurfaceController
4949

5050
## Adding a Product to the Cart
5151

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.
5353

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.
5555

5656
```csharp
5757
namespace Umbraco.Commerce.DemoStore.Dtos;
@@ -63,6 +63,7 @@ public class AddToCartDto
6363
public decimal Quantity { get; set; } = 1;
6464
}
6565
```
66+
6667
2. Add the following action method to your `CartSurfaceController`:
6768

6869
```csharp
@@ -93,7 +94,9 @@ public async Task<IActionResult> AddToCart(AddToCartDto postModel)
9394
return RedirectToCurrentUmbracoPage();
9495
}
9596
```
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`.
97100

98101
```html
99102
<div class="col-12 col-md-4 col-lg-3 mb-5 mb-md-0">
@@ -112,15 +115,15 @@ public async Task<IActionResult> AddToCart(AddToCartDto postModel)
112115
</div>
113116
```
114117

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.
116119

117120
![Add to Cart Success](../images/blendid/product_page_with_notification.png)
118121

119122
### Showing a Cart Count
120123

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.
122125

123-
1. Create a new view component in your project named `CartCountViewComponent`.
126+
1. Create a new view component named `CartCountViewComponent`.
124127

125128
````csharp
126129
[ViewComponent]
@@ -151,7 +154,7 @@ public class CartCountViewComponent : ViewComponent
151154

152155
### Showing Cart Notifications
153156

154-
1. Create a new class in your project named `NotificationModel`.
157+
1. Create a new class named `NotificationModel`.
155158

156159
```csharp
157160
public class NotificationModel
@@ -167,9 +170,9 @@ public enum NotificationType
167170
}
168171
```
169172

170-
2. Create a partial view named `Notification.cshtml`
173+
2. Create a partial view named `Notification.cshtml`.
171174

172-
````csharp
175+
```csharp
173176
@model Umbraco.Commerce.DemoStore.Models.NotificationModel
174177

175178
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css">
@@ -186,10 +189,9 @@ public enum NotificationType
186189
}).showToast();
187190
});
188191
</script>
192+
```
189193

190-
````
191-
192-
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.
193195

194196
```csharp
195197
@{
@@ -209,10 +211,10 @@ public enum NotificationType
209211

210212
## Displaying the Cart
211213

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.
213215

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.
216218

217219
```csharp
218220
@{
@@ -278,16 +280,17 @@ To display the cart, you can create a new view that lists the items in the cart
278280
}
279281
}
280282
```
281-
3. On the front end, navigate to the cart page to view the items in the cart.
282283

283-
![Cart Page](../images/blendid/cart.png)
284+
3. Access the frontend of the website.
285+
4. Navigate to the cart page to view the items in the cart.
284286

287+
![Cart Page](../images/blendid/cart.png)
285288

286289
## Updating a Cart Item
287290

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.
289292

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.
291294

292295
```csharp
293296
namespace Umbraco.Commerce.DemoStore.Dtos;
@@ -343,15 +346,16 @@ public async Task<IActionResult> UpdateCart(UpdateCartDto postModel)
343346
}
344347
```
345348

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.
347351

348352
## Removing a Cart Item
349353

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.
351355

352356
To hook up the remove link, perform the following steps:
353357

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.
355359

356360
```csharp
357361
namespace Umbraco.Commerce.DemoStore.Dtos;
@@ -391,11 +395,12 @@ public async Task<IActionResult> RemoveFromCart(RemoveFromCartDto postModel)
391395
}
392396
```
393397

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.
395400

396401
## Useful Extension Methods
397402

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:
399404

400405
```csharp
401406
public static StoreReadOnly? GetStore(this IPublishedContent content)

0 commit comments

Comments
 (0)