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
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
3
2
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.
6
5
{% endhint %}
7
6
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.
9
10
10
11
```csharp
11
12
@inheritsUmbracoViewPage
@@ -43,56 +44,76 @@ Your view will be similar to the below for the `cart.cshtml` page.
43
44
}
44
45
```
45
46
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
+
47
49
```csharp
48
50
<ahref="@Url.SurfaceAction("RemoveFromCart", "BasketSurface", new { OrderLineId = item.OrderLine.Id })">Remove</a>
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");
130
151
131
-
returnCurrentUmbracoPage();
132
-
}
152
+
returnCurrentUmbracoPage();
153
+
}
133
154
134
-
TempData["SuccessMessage"] ="Item removed";
155
+
TempData["SuccessMessage"] ="Item removed";
135
156
136
-
returnRedirectToCurrentUmbracoPage();
137
-
}
157
+
returnRedirectToCurrentUmbracoPage();
158
+
}
138
159
```
139
160
140
-
- A `trycatch` 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.
143
164
-`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.
146
167
147
168
{% 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.
149
170
{% endhint %}
150
171
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