I have an umbraco v8 solution running vendr 2.4.1 and I'm struggling to get a basic cart implementation working.
I have referred to the docs & the demo project but I cannot seem to get the GetCurrentOrder() method to bring anything back.
The code for my service is as follows:
public class VendrCartService : IVendrCartService
{
private readonly ISessionManager _sessionManager;
private readonly IOrderService _orderService;
private readonly IUnitOfWorkProvider _unitOfWorkProvider;
private readonly Guid _storeId;
public VendrCartService(ISessionManager sessionManager,
IOrderService orderService,
IUnitOfWorkProvider unitOfWorkProvider)
{
_sessionManager = sessionManager;
_orderService = orderService;
_unitOfWorkProvider = unitOfWorkProvider;
_storeId = Guid.Parse("0beb494a-6718-4a88-8a7a-01848149891f");
}
public OrderDto GetCart()
{
var order = _sessionManager.GetCurrentOrder(_storeId);
return order.ToOrderDto();
}
public OrderDto AddToCart(AddToCartDto postModel)
{
using (var unitOfWork = _unitOfWorkProvider.Create())
{
var order = _sessionManager.GetOrCreateCurrentOrder(_storeId)
.AsWritable(unitOfWork)
.AddProduct(postModel.ProductReference, postModel.Quantity);
_orderService.SaveOrder(order);
unitOfWork.Complete();
return order.ToOrderDto();
}
}
}
The AddToCart() method is working fine, I can see the order & order line in the Vendrorder & VendrOrderLine tables, but when I make the call to GetCart() it is not finding this as the saved order and is returning null.
I have hardcoded the storeId temporarily to ensure that my accessing of the umbraco context was not messing anything up.
I have tried injecting IVendrApi instead of ISessionManager for the GetCurrentOrder(), GetOrCreateCurrentOrder(), & SaveOrder() calls, and I have also tried manually calling SetCurrentOrder() before the _orderService.SaveOrder(); line in the snippet above, both with no luck.
I am calling this service from an UmbracoApiController rather than a SurfaceController, so wondering if this has anything to do with it? I have seen in the docs/forums somewhere that a cookie is used to persist the current order, but this doesn't seem to be being set.
You'll want to bare in mind GetCurrentOrder() will only get an order if one exists for the current session, where as GetOrCreateOrder() will get an order if one exists, or create a new on if it doesn't.
When an order is created via the GetOrCreateOrder() method this does indeed set a cookie (as would calling SetCurrentOrder() explicitly). If you are not seeing a cookie in your API response, then you may need to check your configuration.
Is there any specific bit of configuration that I can check or do you have any info on the cookie that is meant to be set?
Does this just happen automatically when called from a surface controller or is there some bit of functionality I am missing? Looking in the demo project nothing seems to be being returned explicitly.
[HttpPost]
public ActionResult AddToCart(AddToCartDto postModel)
{
try
{
using (var uow = _uowProvider.Create())
{
var store = CurrentPage.GetStore();
var order = _sessionManager.GetOrCreateCurrentOrder(store.Id)
.AsWritable(uow)
.AddProduct(postModel.ProductReference, postModel.ProductVariantReference, 1);
_orderService.SaveOrder(order);
uow.Complete();
}
}
catch (ValidationException ex)
{
ModelState.AddModelError("productReference", "Failed to add product to cart");
return CurrentUmbracoPage();
}
TempData["addedProductReference"] = postModel.ProductReference;
return RedirectToCurrentUmbracoPage();
}
I mean, cookies get written into the headers so you won't really see them get "returned", rather they should appear in the request/response headers.
During the cart phase, you should see a cookie like vendr-{GUID}, so on try.vendr.net I have one with the name vendr-b1e61994-b83b-420a-903e-63a7a15942dd, which contains a value like
I'm pretty sure cookies should still be returned on API controllers, though you may need to explicitly send them back if you are using some JS library for communication.
Ok great, I assume the SessionManager has access to the HttpContext and does it in there then but wasn't sure how and when the cookie was meant to be set/returned.
I've checked and I am seeing that cookie being returned, but it is still not being set so you're correct I will need to do something with my ajax request to ensure it is stored.
Appreciate the help, will post the solution here once I've done it for anyone else who may encounter the same issue.
Embarrassingly, the cookie was being returned fine but wasn't being set correctly as I was hitting the http version of the local development site rather than https without realising.
This was causing chrome to not store the cookie for security reasons!
GetCurrentOrder is returning null
Hi
I have an umbraco v8 solution running vendr 2.4.1 and I'm struggling to get a basic cart implementation working.
I have referred to the docs & the demo project but I cannot seem to get the GetCurrentOrder() method to bring anything back.
The code for my service is as follows:
The AddToCart() method is working fine, I can see the order & order line in the Vendrorder & VendrOrderLine tables, but when I make the call to GetCart() it is not finding this as the saved order and is returning null.
I have hardcoded the storeId temporarily to ensure that my accessing of the umbraco context was not messing anything up.
I have tried injecting IVendrApi instead of ISessionManager for the GetCurrentOrder(), GetOrCreateCurrentOrder(), & SaveOrder() calls, and I have also tried manually calling SetCurrentOrder() before the _orderService.SaveOrder(); line in the snippet above, both with no luck.
I am calling this service from an UmbracoApiController rather than a SurfaceController, so wondering if this has anything to do with it? I have seen in the docs/forums somewhere that a cookie is used to persist the current order, but this doesn't seem to be being set.
Any help with this would be appreciated, thanks.
Andrew
Hi Andrew,
You'll want to bare in mind
GetCurrentOrder()
will only get an order if one exists for the current session, where asGetOrCreateOrder()
will get an order if one exists, or create a new on if it doesn't.When an order is created via the
GetOrCreateOrder()
method this does indeed set a cookie (as would callingSetCurrentOrder()
explicitly). If you are not seeing a cookie in your API response, then you may need to check your configuration.Thanks Matt.
Is there any specific bit of configuration that I can check or do you have any info on the cookie that is meant to be set?
Does this just happen automatically when called from a surface controller or is there some bit of functionality I am missing? Looking in the demo project nothing seems to be being returned explicitly.
I mean, cookies get written into the headers so you won't really see them get "returned", rather they should appear in the request/response headers.
During the cart phase, you should see a cookie like
vendr-{GUID}
, so on try.vendr.net I have one with the namevendr-b1e61994-b83b-420a-903e-63a7a15942dd
, which contains a value likeI'm pretty sure cookies should still be returned on API controllers, though you may need to explicitly send them back if you are using some JS library for communication.
Ok great, I assume the SessionManager has access to the HttpContext and does it in there then but wasn't sure how and when the cookie was meant to be set/returned.
I've checked and I am seeing that cookie being returned, but it is still not being set so you're correct I will need to do something with my ajax request to ensure it is stored.
Appreciate the help, will post the solution here once I've done it for anyone else who may encounter the same issue.
Embarrassingly, the cookie was being returned fine but wasn't being set correctly as I was hitting the http version of the local development site rather than https without realising.
This was causing chrome to not store the cookie for security reasons!
Thanks for the help.
is working on a reply...