Copied to clipboard

Flag this post as spam?

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


  • Kenni Rau 8 posts 109 karma points
    Mar 12, 2021 @ 08:23
    Kenni Rau
    0

    Update order status

    I have created an order and would like to change the order status, I have tried setting the status before and after the order is Finalized, but its not being saved.

    Here is the code:

            using (var uow = _uniteOfWorkProvider.Create())
            {
                var order = _sessionManager.GetOrCreateCurrentOrder(store.Id).AsWritable(uow);
                var orderStatus = _orderStatusService.GetOrderStatus(store.Id, "newReturnOrder");
                order.SetOrderStatus(orderStatus);
    
                _orderService.SaveOrder(order);
                uow.Complete();
            }
    

    I can see that the order object is getting updated, but its not being saved.

  • Matt Brailsford 4123 posts 22194 karma points MVP 9x c-trib
    Mar 12, 2021 @ 09:09
    Matt Brailsford
    0

    Hi Kenni,

    The code on it's own seems correct. Where is this code running though? in a controller? or event handler?

    Generally if something doesn't persist my first port of call would be to check all unit's of work have uow.Complete() called on them as if you have any event handlers that don't do this, it can prevent a whole pipeline from persisting.

  • Kenni Rau 8 posts 109 karma points
    Mar 12, 2021 @ 09:22
    Kenni Rau
    0

    Hi Matt,

    It's running in a SurfaceController which I'm using to create return orders.

    I'm have a few unit's of work running for: (Had to split them else they would not save either)

    • Assigning order to customer
    • Add products
    • Set shipping method
    • Set payment method and authorize it
    • Update order properties
    • Set status

    They all have uow.Complete(); called on them the order is created correctly except for the status still being new.

  • Matt Brailsford 4123 posts 22194 karma points MVP 9x c-trib
    Mar 12, 2021 @ 10:59
    Matt Brailsford
    0

    Are all those actions occurring at the same time just within their own units of work?

  • Kenni Rau 8 posts 109 karma points
    Mar 12, 2021 @ 11:13
    Kenni Rau
    0

    They are all within the same try catch, but are running in sequence

                using (var uow = _uniteOfWorkProvider.Create())
                {
                    var order = _sessionManager.GetOrCreateCurrentOrder(store.Id).AsWritable(uow);
    
                    var paymentMethods = VendrApi.Instance.GetPaymentMethodsAllowedIn(order.PaymentInfo.CountryId.Value);
                    var zeroValuePaymentMethod = paymentMethods.FirstOrDefault(x => x.Alias == VendrCheckoutConstants.PaymentMethods.Aliases.ZeroValue);
                    order.SetPaymentMethod(zeroValuePaymentMethod);
    
                    _orderService.SaveOrder(order);
    
                    uow.Complete();
                }
    
                using (var uow = _uniteOfWorkProvider.Create())
                {
                    var order = _sessionManager.GetOrCreateCurrentOrder(store.Id).AsWritable(uow);
    
                    var orderNumberGenerator = new ReturnOrderNumberGenerator(_storeService);
                    order.InitializeTransaction(orderNumberGenerator);
                    order.Finalize(order.TotalPrice, Guid.NewGuid().ToString("N"), PaymentStatus.Authorized);
                    _orderService.SaveOrder(order);
    
                    uow.Complete();
                }
    
                using (var uow = _uniteOfWorkProvider.Create())
                {
                    var originOrder = _orderService.GetOrder(postModel.Id).AsWritable(uow);
                    var productDetailsJson = UpdateProductDetails(originOrder.Properties["productDetailsList"], postModel.OrderLines);
                    originOrder.SetProperty("productDetailsList", productDetailsJson);
    
                    _orderService.SaveOrder(originOrder);
                    uow.Complete();
                }
    
                using (var uow = _uniteOfWorkProvider.Create())
                {
                    var order = _sessionManager.GetOrCreateCurrentOrder(store.Id).AsWritable(uow);
                    var orderStatus = _orderStatusService.GetOrderStatus(store.Id, "newReturnOrder");
                    order.SetOrderStatus(orderStatus);
    
                    _orderService.SaveOrder(order);
                    uow.Complete();
                }
    

    All other snippets are saving the data correct, only the status that is being ignored.

  • Matt Brailsford 4123 posts 22194 karma points MVP 9x c-trib
    Mar 12, 2021 @ 11:34
    Matt Brailsford
    100

    Ok, well, these really all should be performed within a single unit of work and using a single writable order to update all of these.

    By using individual units of work you have the potential that you can leave your system an a middle state if early UoW's succeed, but then the later ones fail. The point of a UoW is to act as a transaction and ensure everything happens, or nothing happens.

    I think your potential issue here might be that you are finalizing an order in an early stage, and then using GetOrCreateCurrentOrder in a later stage, however because you have finalized your order, this method will give you a new order. Once an order is finalized, if you need to get it from the session manager again, you need to call GetCurrentFinalizedOrder instead.

    As I say though, if these were all in one UoW, you can be sure that the order is the same order throughout.

  • Kenni Rau 8 posts 109 karma points
    Mar 12, 2021 @ 12:12
    Kenni Rau
    0

    Thanks Matt, using GetCurrentFinalizedOrder instead did the trick. Got tunnelvision and forgot about that method...

    I had all of the code in the same uow in the beginning, but then most of it did not get saved, so split them out and it worked. Will test some more on using one uow.

  • Matt Brailsford 4123 posts 22194 karma points MVP 9x c-trib
    Mar 12, 2021 @ 13:47
    Matt Brailsford
    1

    It would probably be best to try and work out why it didn't work in the single UoW really, but if what you have works, just know you can get into an inconsistent state should any one UoW fail. If you are happy with that, then cool. I just need to point it out and make it clear to yourself and anyone else who has a similar problem.

  • Kenni Rau 8 posts 109 karma points
    Mar 12, 2021 @ 15:10
    Kenni Rau
    0

    Yea I understand that, got it running in one uow now. Using _orderService.SaveOrder(order) after every update instead of using separate uow.

Please Sign in or register to post replies

Write your reply to:

Draft