Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Andrew 23 posts 156 karma points
    Jan 26, 2023 @ 15:26
    Andrew
    0

    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:

        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.

    Any help with this would be appreciated, thanks.

    Andrew

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jan 26, 2023 @ 15:36
    Matt Brailsford
    0

    Hi Andrew,

    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.

  • Andrew 23 posts 156 karma points
    Jan 26, 2023 @ 15:48
    Andrew
    0

    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.

    [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();
        }
    
  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jan 26, 2023 @ 16:47
    Matt Brailsford
    100

    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

    dpc=af697207-d370-4aee-824c-15711d43a9f2&dsc=af697207-d370-4aee-824c-15711d43a9f2&dtc=17a2eca0-d21f-462a-8915-8b2606661efd&dc=30b62176-6a9e-4a51-b2f0-7ce6c80a461a&co=52d0da42-1522-4389-a080-0185eef6b069
    

    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.

  • Andrew 23 posts 156 karma points
    Jan 27, 2023 @ 08:49
    Andrew
    1

    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.

  • Andrew 23 posts 156 karma points
    Jan 27, 2023 @ 16:12
    Andrew
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft