Copied to clipboard

Flag this post as spam?

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


These support forums are now closed for new topics and comments.
Please head on over to http://eureka.ucommerce.net/ for support.

  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    Oct 29, 2014 @ 14:58
    Bjarne Fyrstenborg
    0

    Return Order Details as json

    Hi..

    How can I return order details as json in via the API?

    E.g. I can do this to return json about an order using Newtonsoft.Json:

    public string GetOrderDetails(int orderId)
    {
        var order = PurchaseOrder.All()
            .Where(x => x.OrderStatus.OrderStatusId != 1 && x.OrderId == orderId)
            .Select(x => new { OrderId = x.OrderId, OrderDate = x.CreatedDate, OrderStatusId = x.OrderStatus.OrderStatusId, OrderStatus = x.OrderStatus.Name })
            .FirstOrDefault();
    
        return JsonConvert.SerializeObject(order);
    }

    However this won't work just to add the OrderLines:

    public string GetOrderDetails(int orderId)
    {
        var order = PurchaseOrder.All()
            .Where(x => x.OrderStatus.OrderStatusId != 1 && x.OrderId == orderId)
            .Select(x => new { OrderId = x.OrderId, OrderDate = x.CreatedDate, OrderStatusId = x.OrderStatus.OrderStatusId, OrderStatus = x.OrderStatus.Name, OrderLines = x.OrderLines })
            .FirstOrDefault();
    
        return JsonConvert.SerializeObject(order);
    }

     

    /Bjarne

  • Morten Skjoldager 440 posts 1499 karma points
    Oct 30, 2014 @ 14:44
    Morten Skjoldager
    0

    Do you get any errors? 

  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    Oct 30, 2014 @ 18:34
    Bjarne Fyrstenborg
    0

    I got an error about the serialization of the object when the LINQ expression contained OrderLines = x.OrderLines
    Also it shouldn't be the OrderId but actually the OrderNumber I want to search for.

    Now I have this, which returns basic info about the order and the an array of the orderlines. I'm not sure if it could be shorten or there is a way to return all properties in each OrderLine to strings? Right now I select some specific properties..

    public string GetOrderDetails(string orderNumber)
    {
        var purchaseOrder =
            PurchaseOrder.All().Single(x => x.OrderNumber == orderNumber && x.OrderStatus.OrderStatusId != 1);
    
        var orderLines = new List<string[]>();
        foreach (OrderLine ol in purchaseOrder.OrderLines)
        {
            var li = new string[]
            {
                ol.OrderLineId.ToLowerInvariantString(),
                ol.Sku,
                ol.ProductName,
                ol.Price.ToLowerInvariantString(),
                ol.Quantity.ToLowerInvariantString()
            };
    
            orderLines.Add(li);
        }
    
        var o = new List<object>
        {
            purchaseOrder.OrderId,
            purchaseOrder.CreatedDate,
            purchaseOrder.OrderStatus.OrderStatusId,
            purchaseOrder.OrderStatus.Name,
            orderLines
        };
    
        return JsonConvert.SerializeObject(o);
    }

     

    /Bjarne

  • Morten Skjoldager 440 posts 1499 karma points
    Oct 31, 2014 @ 09:41
    Morten Skjoldager
    0

    As long as it works i guess you're good to go :) 

Please Sign in or register to post replies

Write your reply to:

Draft