-
Notifications
You must be signed in to change notification settings - Fork 813
Create add-item article #6521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Create add-item article #6521
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3ec8908
Create add-item article
mr-shellychauhan 43c323a
Update 13/umbraco-commerce/how-to-guides/add-item
mr-shellychauhan db7a256
Update 13/umbraco-commerce/how-to-guides/add-item
mr-shellychauhan 2ca5381
Update 13/umbraco-commerce/how-to-guides/add-item
mr-shellychauhan dbe586a
Update 13/umbraco-commerce/how-to-guides/add-item
mr-shellychauhan f575d89
Update 13/umbraco-commerce/how-to-guides/add-item
mr-shellychauhan 71e4a92
Update 13/umbraco-commerce/how-to-guides/add-item
mr-shellychauhan a9deb42
Update 13/umbraco-commerce/how-to-guides/add-item
mr-shellychauhan c13ad58
Update 13/umbraco-commerce/how-to-guides/add-item
mr-shellychauhan aeca817
Update add-item
mr-shellychauhan e20da06
Corrected grammar and added more context
sofietoft 440a328
Update and rename add-item to add-item.md
mr-shellychauhan ed383f4
Fixed long sentence
sofietoft 70bfb75
Add article to SUMMARY.md
sofietoft 97436f6
Update SUMMARY.md
mr-shellychauhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| --- | ||
| description: How-To Guide to add an item to your cart. | ||
| --- | ||
|
|
||
| To add an item to the cart, you first need to setup Umbraco with a store and add the relevant properties (i.e. Price and Stock see https://docs.umbraco.com/umbraco-commerce/10.commerce.latest/key-concepts/umbraco-properties) to allow the store to interact with Umbraco. | ||
|
|
||
| ## Add a product to the Cart | ||
| You will need the Frontend to be setup to add allow an item to be added to the cart. This can be done by adding a button to the frontend that will call the Action to add the item to the cart. | ||
|
|
||
| Create a new View, we will call it product.cshtml with the below code | ||
|
|
||
| ```csharp | ||
mr-shellychauhan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @{ | ||
| var store = Model.Value<StoreReadOnly>("store", fallback: Fallback.ToAncestors); | ||
| var product = CommerceApi.Instance.GetProduct(store.Id, Model.Key.ToString(), "en-GB"); | ||
| var price = product.TryCalculatePrice(); | ||
mr-shellychauhan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| ``` | ||
|
|
||
| - You will need to access the store so you have access to the relevant properties for your product such as price. The store has a fallback property allowing you to traverse up the tree to find the store. | ||
| - We retrieve the product based on the store and a reference for the product (the 'productReference' in this instance is coming from the Model which is a single product). | ||
| - The Product is returned as a ProductSnapshot which is Umbraco Commerce obtaining the page ID and carrying out necessary processes to bring in the data which it can use for further processing. | ||
| - Finally we need to calculate the price which is then displayed without VAT (although can be also displayed with VAT) | ||
|
|
||
| To display this we need to add some markup or at least amend it to include a button to add an item. In the same file add the below | ||
|
|
||
| ```csharp | ||
| @using (Html.BeginUmbracoForm("AddToCart", "CartSurface")) | ||
| { | ||
| @Html.Hidden("productReference", Model.Key.ToString()) | ||
| <h1>@Model.ProductTitle</h1> | ||
mr-shellychauhan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| <h2>@Model.ProductDescription</h2> | ||
mr-shellychauhan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| <p>Our price excluding VAT <strong>@price.Result?.WithoutTax.ToString("C0") </strong></p> | ||
mr-shellychauhan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (@Model.Stock == 0) | ||
mr-shellychauhan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| <p>Sorry, out of stock</p> | ||
| } | ||
| else | ||
| { | ||
| <button type="submit">Add to Basket</button> | ||
| } | ||
|
|
||
| @await Html.PartialAsync("...../Feedback.cshtml") | ||
mr-shellychauhan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| ``` | ||
|
|
||
| The hidden field is using the productReference to be passed across to the Controller. | ||
|
|
||
| {% hint style="warning" %} | ||
mr-shellychauhan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ModelsBuilder is used to access the properties of the page. | ||
| If you are not using ModelsBuilder then you will need to use the following code to access the properties | ||
|
|
||
| Model.Value<IPublishedContent>("nameOfProperty") | ||
|
|
||
| {% endhint %} | ||
|
|
||
| ## Adding the Controller | ||
|
|
||
| Create a new Controller called CartSurfaceController.cs | ||
|
|
||
| {% hint style="warning" %} | ||
|
|
||
| The namespaces used in this Controller are important and need to be included. | ||
|
|
||
| using Microsoft.AspNetCore.Mvc; | ||
| using Umbraco.Cms.Core.Cache; | ||
| using Umbraco.Cms.Core.Logging; | ||
| using Umbraco.Cms.Core.Models.PublishedContent; | ||
| using Umbraco.Cms.Core.Routing; | ||
| using Umbraco.Cms.Core.Services; | ||
| using Umbraco.Cms.Core.Web; | ||
| using Umbraco.Cms.Infrastructure.Persistence; | ||
| using Umbraco.Cms.Web.Website.Controllers; | ||
| using Umbraco.Commerce.Common.Validation; | ||
| using Umbraco.Commerce.Core.Api; | ||
| using Umbraco.Commerce.Core.Models; | ||
| using Umbraco.Commerce.Extensions; | ||
| using Umbraco.Extensions; | ||
|
|
||
| {% endhint %} | ||
|
|
||
| ```csharp | ||
| public class CartSurfaceController : SurfaceController | ||
| { | ||
| public CartSurfaceController(IUmbracoContextAccessor umbracoContextAccessor, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, AppCaches appCaches, IProfilingLogger profilingLogger, IPublishedUrlProvider publishedUrlProvider, IUmbracoCommerceApi commerceApi) : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider) | ||
| { | ||
| _commerceApi = commerceApi; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| The equivalent code for having this as a Primary Constructor | ||
|
|
||
| ```csharp | ||
| public class CartSurfaceController(IUmbracoContextAccessor umbracoContextAccessor, IUmbracoDatabaseFactory databaseFactory, ServiceContext services, AppCaches appCaches, IProfilingLogger profilingLogger, IPublishedUrlProvider publishedUrlProvider) : SurfaceController(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider) | ||
| { | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
| The CartDto is a class that is used to pass the productReference across to the Controller. This is a simple class that has a property for the productReference. | ||
|
|
||
| ```csharp | ||
| public class CartDto | ||
| { | ||
| public string ProductReference { get; set; } | ||
| } | ||
| ``` | ||
|
|
||
| We now need to add the Action in order to add the item to the cart. This will be called when the button is clicked. | ||
|
|
||
| ```csharp | ||
| [HttpPost] | ||
| public IActionResult AddToBasket(CartDto cart) | ||
| { | ||
| commerceApi.Uow.Execute(uow => | ||
| { | ||
| var store = CurrentPage.Value<StoreReadOnly>("store", fallback: Fallback.ToAncestors); | ||
|
|
||
| if (store == null) return; | ||
|
|
||
| try | ||
| { | ||
| var order = commerceApi.GetOrCreateCurrentOrder(store.Id) | ||
| .AsWritable(uow) | ||
| .AddProduct(cart.ProductReference, 1); | ||
|
|
||
| commerceApi.SaveOrder(order); | ||
|
|
||
| uow.Complete(); | ||
|
|
||
| TempData["SuccessFeedback"] = "Product added to cart"; | ||
| return RedirectToCurrentUmbracoPage(); | ||
| } | ||
| catch (ValidationException ve) | ||
| { | ||
| throw new ValidationException(ve.Errors); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.Error(ex, "An error occurred."); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| - store variable is used to access the store to get the store ID. | ||
| - A try catch block is used to capture any errors that may occur when adding the item to the cart, including any validation errors. | ||
| - order is used to retrieve the current order if one exists or create a new order against the store found. In the Commerce Api everything is read-only for performance so we need to make it writable in order to add the product. | ||
| - AddProduct is called and the passed in productReference is passed across along with the quantity. | ||
| - SaveOrder is called to save the order. | ||
| - TempData is used to store a message to be displayed to the user if the product has been added to the cart. | ||
|
|
||
| {% hint style="warning" %} | ||
| Umbraco Commerce uses the Unit of Work pattern in order to complete saviing 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. | ||
| {% endhint %} | ||
|
|
||
| Finally, we need to add the TempData to display a message to the user that the product has been added to the cart. | ||
|
|
||
| ## Add a partial view to display the message | ||
|
|
||
| Create a new partial view called Feedback.cshtml | ||
|
|
||
| ```csharp | ||
| @Html.ValidationSummary(true, "", new { @class = "danger" }) | ||
|
|
||
| @{ | ||
| var success = TempData["SuccessFeedback"]?.ToString(); | ||
|
|
||
| if (!string.IsNullOrWhiteSpace(success)) | ||
| { | ||
| <div class="success">@success</div> | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Run the application, click the button and the product will be added to the cart with a message displayed to the user. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.