From 47037f64887eff7ffd681e8d5efc5b96b015e280 Mon Sep 17 00:00:00 2001 From: Tommy Marshment-Howell Date: Fri, 8 Mar 2019 09:53:42 -0600 Subject: [PATCH] Allow guest customer POST to /api/shopping_cart_items --- .../Controllers/ShoppingCartItemsController.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Nop.Plugin.Api/Controllers/ShoppingCartItemsController.cs b/Nop.Plugin.Api/Controllers/ShoppingCartItemsController.cs index da2d6bb..e75c993 100644 --- a/Nop.Plugin.Api/Controllers/ShoppingCartItemsController.cs +++ b/Nop.Plugin.Api/Controllers/ShoppingCartItemsController.cs @@ -23,6 +23,7 @@ using Nop.Services.Stores; using Nop.Plugin.Api.Helpers; using Nop.Core; +using Nop.Core.Domain.Customers; namespace Nop.Plugin.Api.Controllers { @@ -41,6 +42,7 @@ public class ShoppingCartItemsController : BaseApiController private readonly IProductAttributeConverter _productAttributeConverter; private readonly IDTOHelper _dtoHelper; private readonly IStoreContext _storeContext; + private readonly ICustomerService _customerService; public ShoppingCartItemsController(IShoppingCartItemApiService shoppingCartItemApiService, IJsonFieldsSerializer jsonFieldsSerializer, @@ -75,6 +77,7 @@ public ShoppingCartItemsController(IShoppingCartItemApiService shoppingCartItemA _productAttributeConverter = productAttributeConverter; _dtoHelper = dtoHelper; _storeContext = storeContext; + _customerService = customerService; } /// @@ -197,8 +200,16 @@ public IActionResult CreateShoppingCartItem([ModelBinder(typeof(JsonModelBinder< return Error(); } + //Add guest customer if none is passed + Customer customer = null; + if(shoppingCartItemDelta.Dto.Id == 0) + { + customer = _customerService.InsertGuestCustomer(); + } + var newShoppingCartItem = _factory.Initialize(); shoppingCartItemDelta.Merge(newShoppingCartItem); + // We know that the product id and customer id will be provided because they are required by the validator. // TODO: validate @@ -209,7 +220,10 @@ public IActionResult CreateShoppingCartItem([ModelBinder(typeof(JsonModelBinder< return Error(HttpStatusCode.NotFound, "product", "not found"); } - var customer = CustomerService.GetCustomerById(newShoppingCartItem.CustomerId); + if (customer == null) + { + customer = CustomerService.GetCustomerById(newShoppingCartItem.CustomerId); + } if (customer == null) {