Skip to content
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

Allow guest customer POST to /api/shopping_cart_items #189

Open
wants to merge 1 commit into
base: nopCommerce_4.10
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion Nop.Plugin.Api/Controllers/ShoppingCartItemsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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,
Expand Down Expand Up @@ -75,6 +77,7 @@ public ShoppingCartItemsController(IShoppingCartItemApiService shoppingCartItemA
_productAttributeConverter = productAttributeConverter;
_dtoHelper = dtoHelper;
_storeContext = storeContext;
_customerService = customerService;
}

/// <summary>
Expand Down Expand Up @@ -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
Expand All @@ -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)
{
Expand Down