Skip to content

Commit 661369a

Browse files
authored
Correct grammar and context
1 parent 1af65a8 commit 661369a

File tree

1 file changed

+76
-55
lines changed

1 file changed

+76
-55
lines changed
Lines changed: 76 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
## Delete item from cart
2-
Following on from the previous guide on [updating an item in the cart](/umbraco-commerce/how-to-guides/update-cart.md), we will now look at how to delete an item from the cart.
1+
# Delete item from cart
32

4-
{% hint style="warning" %}
5-
This tutorial focuses only on the delete functionality. Please see the other guides for more information for areas not covered here.
3+
{% hint style="info" %}
4+
This guide builds on the guide on [update-cart.md). It is recommended to follow that guide before starting this one.
65
{% endhint %}
76

8-
Your view will be similar to the below for the `cart.cshtml` page.
7+
This will teach you how to delete an item from the cart.
8+
9+
Your view for the `cart.cshtml` page will be similar the example below.
910

1011
```csharp
1112
@inherits UmbracoViewPage
@@ -43,56 +44,76 @@ Your view will be similar to the below for the `cart.cshtml` page.
4344
}
4445
```
4546

46-
- The below code allows the Umbraco `SurfaceAction` to call `RemoveFromCart` when the link is clicked as well as passing the `OrderLineId`.
47+
The code below allows the Umbraco `SurfaceAction` to call `RemoveFromCart` when the link is clicked. It will also pass the `OrderLineId`.
48+
4749
```csharp
4850
<a href="@Url.SurfaceAction("RemoveFromCart", "BasketSurface", new { OrderLineId = item.OrderLine.Id })">Remove</a>
4951
```
52+
5053
## Adding the Controller
5154

55+
For the button to work, you need to add some functionality via a Controller.
56+
5257
Create a new Controller called `CartSurfaceController.cs`
5358

5459
{% hint style="warning" %}
5560

5661
The namespaces used in this Controller are important and need to be included.
5762

58-
using Microsoft.AspNetCore.Mvc;
59-
using Umbraco.Cms.Core.Cache;
60-
using Umbraco.Cms.Core.Logging;
61-
using Umbraco.Cms.Core.Models.PublishedContent;
62-
using Umbraco.Cms.Core.Routing;
63-
using Umbraco.Cms.Core.Services;
64-
using Umbraco.Cms.Core.Web;
65-
using Umbraco.Cms.Infrastructure.Persistence;
66-
using Umbraco.Cms.Web.Website.Controllers;
67-
using Umbraco.Commerce.Common.Validation;
68-
using Umbraco.Commerce.Core.Api;
69-
using Umbraco.Commerce.Core.Models;
70-
using Umbraco.Commerce.Extensions;
71-
using Umbraco.Extensions;
63+
```
64+
using Microsoft.AspNetCore.Mvc;
65+
using Umbraco.Cms.Core.Cache;
66+
using Umbraco.Cms.Core.Logging;
67+
using Umbraco.Cms.Core.Models.PublishedContent;
68+
using Umbraco.Cms.Core.Routing;
69+
using Umbraco.Cms.Core.Services;
70+
using Umbraco.Cms.Core.Web;
71+
using Umbraco.Cms.Infrastructure.Persistence;
72+
using Umbraco.Cms.Web.Website.Controllers;
73+
using Umbraco.Commerce.Common.Validation;
74+
using Umbraco.Commerce.Core.Api;
75+
using Umbraco.Commerce.Core.Models;
76+
using Umbraco.Commerce.Extensions;
77+
using Umbraco.Extensions;
78+
```
7279
7380
{% endhint %}
7481
7582
```csharp
7683
public class CartSurfaceController : SurfaceController
7784
{
78-
public CartSurfaceController(IUmbracoContextAccessor umbracoContextAccessor, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, AppCaches appCaches, IProfilingLogger profilingLogger, IPublishedUrlProvider publishedUrlProvider, IUmbracoCommerceApi commerceApi) : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
85+
public CartSurfaceController(IUmbracoContextAccessor umbracoContextAccessor,
86+
IUmbracoDatabaseFactory databaseFactory,
87+
ServiceContext services,
88+
AppCaches appCaches,
89+
IProfilingLogger profilingLogger,
90+
IPublishedUrlProvider publishedUrlProvider,
91+
IUmbracoCommerceApi commerceApi)
92+
: base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
7993
{
8094
_commerceApi = commerceApi;
8195
}
8296
}
8397
```
8498

85-
The equivalent code for having this as a Primary Constructor
99+
The example below is the equivalent code for having this as a Primary Constructor:
86100

87101
```csharp
88-
public class CartSurfaceController(IUmbracoContextAccessor umbracoContextAccessor, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, AppCaches appCaches, IProfilingLogger profilingLogger, IPublishedUrlProvider publishedUrlProvider, IUmbracoCommerceApi commerceApi) : SurfaceController(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
102+
public class CartSurfaceController(IUmbracoContextAccessor umbracoContextAccessor,
103+
IUmbracoDatabaseFactory databaseFactory,
104+
ServiceContext services,
105+
AppCaches appCaches,
106+
IProfilingLogger profilingLogger,
107+
IPublishedUrlProvider publishedUrlProvider,
108+
IUmbracoCommerceApi commerceApi)
109+
: SurfaceController(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
89110
{
90111
}
91112
```
92113

93114

94115

95-
The `CartDto` is a class that is used to pass data to the Controller. In this instance it is passing over the OrderLineId.
116+
The `CartDto` is a class used to pass data to the Controller. In this instance, it passes over the `OrderLineId`.
96117

97118
```csharp
98119
public class CartDto
@@ -101,51 +122,51 @@ The `CartDto` is a class that is used to pass data to the Controller. In this in
101122
}
102123
```
103124

104-
We now need to add the `Action` in order to delete the item from the cart. This will be called when the button is clicked.
125+
You need to add the `Action` to delete the item from the cart. This will be called when the button is clicked.
105126

106127
```csharp
107-
[HttpGet]
108-
public IActionResult RemoveFromCart(CartDto cart)
128+
[HttpGet]
129+
public IActionResult RemoveFromCart(CartDto cart)
130+
{
131+
try
132+
{
133+
_commerceApi.Uow.Execute(uow =>
109134
{
110-
try
111-
{
112-
_commerceApi.Uow.Execute(uow =>
113-
{
114-
var store = CurrentPage?.Value<StoreReadOnly>("store", fallback: Fallback.ToAncestors);
135+
var store = CurrentPage?.Value<StoreReadOnly>("store", fallback: Fallback.ToAncestors);
115136

116-
if (store == null) return;
137+
if (store == null) return;
117138

118-
var order = _commerceApi.GetOrCreateCurrentOrder(store.Id)
119-
.AsWritable(uow)
120-
.RemoveOrderLine(cart.OrderLineId);
139+
var order = _commerceApi.GetOrCreateCurrentOrder(store.Id)
140+
.AsWritable(uow)
141+
.RemoveOrderLine(cart.OrderLineId);
121142

122-
_commerceApi.SaveOrder(order);
143+
_commerceApi.SaveOrder(order);
123144

124-
uow.Complete();
125-
});
126-
}
127-
catch (ValidationException)
128-
{
129-
ModelState.AddModelError(string.Empty, "Failed to remove product from cart");
145+
uow.Complete();
146+
});
147+
}
148+
catch (ValidationException)
149+
{
150+
ModelState.AddModelError(string.Empty, "Failed to remove product from cart");
130151

131-
return CurrentUmbracoPage();
132-
}
152+
return CurrentUmbracoPage();
153+
}
133154

134-
TempData["SuccessMessage"] = "Item removed";
155+
TempData["SuccessMessage"] = "Item removed";
135156

136-
return RedirectToCurrentUmbracoPage();
137-
}
157+
return RedirectToCurrentUmbracoPage();
158+
}
138159
```
139160

140-
- A `try catch` block is used to capture any validation errors that may occur when updating items in the cart.
141-
- `store` variable is used to access the store to retrieve the store ID.
142-
- `order` is used to retrieve the current order. In the Commerce Api everything is read-only for performance so we need to make it writable in order to add the product.
161+
- A `try-catch` block captures any validation errors that may occur when updating items in the cart.
162+
- The `store` variable is used to access the store to retrieve the store ID.
163+
- `order` is used to retrieve the current order. In the Commerce API, everything is read-only for performance so you need to make it writable to add the product.
143164
- `SaveOrder` is called to save the order.
144-
- If there are any validation errors, they are added to ModelState error and the user is redirected back to the current page.
145-
- `TempData` is used to store a message to be displayed to the user if the product has been succesfully updated.
165+
- If there are any validation errors, they are added to a `ModelState` error, and the user is redirected back to the current page.
166+
- `TempData` stores a message to be displayed to the user if the product has been successfully updated.
146167

147168
{% hint style="warning" %}
148-
Umbraco Commerce uses the Unit of Work pattern in order to complete saving the item (uow.Complete). When retrieving or saving data ideally you would want the entire transaction to be committed however if there is an error then nothing is changed on the database.
169+
Umbraco Commerce uses the Unit of Work pattern to complete saving the item (`uow.Complete`). When retrieving or saving data ideally you would want the entire transaction to be committed. However, if there is an error nothing is changed on the database.
149170
{% endhint %}
150171

151-
If you have followed the 'Add item to cart' article then run the application, add an item to your cart and navigate to your cart.cshtml page. Clicking the `Remove Item` button would delete the the item in your cart and display a success message.
172+
If you have followed the [Add item to cart](add-item.md) article, run the application, add an item to your cart, and navigate to your `cart.cshtml` page. Clicking the `Remove Item` button will delete the item in your cart and display a message.

0 commit comments

Comments
 (0)