Copied to clipboard

Flag this post as spam?

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


  • Tor Langlo 123 posts 320 karma points
    Dec 22, 2018 @ 07:32
    Tor Langlo
    0

    Finding an order by its order number

    I have not found a way to look up an order by the order number. Does it exist?

    Here's my non-optimal method, requires two database roundtrips, and having sql in the application code is of course not ideal:

    public static Order TryFindOrder(long storeId, string orderNo)
    {
        if (string.IsNullOrWhiteSpace(orderNo))
            return null;
        if (orderNo.Length > 20)
            return null;
        if (orderNo.ContainsAny(new char[] {';', ',', ' '}))
            return null;
        var db = ApplicationContext.Current.DatabaseContext.Database;
        var orders = db.Fetch<Guid>(string.Format(
            "Select Id FROM TeaCommerce_Order WHERE StoreId = {0} and OrderNumber = '{1}'",
            storeId, orderNo));
        if (orders == null || orders.Count != 1)
            return null;
        return TC.GetOrder(storeId, orders[0]);
    }
    

    -Tor

  • Matt Brailsford 2189 posts 10695 karma points MVP 6x c-trib
    Dec 23, 2018 @ 09:37
    Matt Brailsford
    0

    Hey Tor,

    You are right, there doesn't look to be 1 API at the moment, but there is some code to at least make this a little nicer.

    Ultimately, there is an API to get an ID from a order number, but you'll need to request it from the OrderService itself (which the TC helper wraps) so you can use something like the following:

    var orderId = OrderService.Instance.GetOrderId(storeId, orderNumber);
    if (orderId != null) {
        var order = OrderService,Instance.GetOrder(storeId, orderId);
    }
    

    Ultimately though, this is just doing what you are doing, but it just makes it a little nicer in your codebase.

    I've raised an issue on the Tea Commerce Umbraco UI project to add a method to get an order by order number from the TC helper though, so I'll look to add this in the future https://github.com/TeaCommerce/Tea-Commerce-for-Umbraco/issues/69

    Hope this helps.

    Matt

  • Tor Langlo 123 posts 320 karma points
    Dec 24, 2018 @ 19:45
    Tor Langlo
    0

    Thanks, Mark!

    I missed the GetOrderId method, that makes it a lot easier :-)

    Do those two methods (GetOrderId, then GetOrder) both hit the database, or do they take advantage of any caching inside the Api/Repository service?

    And something that maybe should be added to the documentation in general, which methods do their own checks against malformed parameters (SQL injection attacks, etc.)? When is it the application developer's responsibility to do this?

    -Tor

  • Tor Langlo 123 posts 320 karma points
    Dec 26, 2018 @ 18:43
    Tor Langlo
    0

    After further inspection it looks like the GetOrderId method uses CartId (instead of OrderNumber) as its input parameter so that will not work.

    But I was able to simplify and improve my temporary method as follows:

        public static Order TryFindOrder(long storeId, string orderNo)
        {
            var db = ApplicationContext.Current.DatabaseContext.Database;
            Guid? orderId = db.ExecuteScalar<Guid?>(
                "Select Id FROM TeaCommerce_Order WHERE StoreId = @0 and OrderNumber = @1",
                storeId, orderNo);
            return orderId != null ? TC.GetOrder(storeId, orderId.Value) : null;
        }
    

    This should take care of input parameter validation as well as being shorter and possibly faster.

    -Tor

  • Matt Brailsford 2189 posts 10695 karma points MVP 6x c-trib
    Dec 27, 2018 @ 11:22
    Matt Brailsford
    0

    Ahh, doh!

    Glad you were able to come up with a nicer solution. I'll see what I can do about improving this and the documentation around it (I'm currently moving the docs to github to make working on them a bit easier).

Please Sign in or register to post replies

Write your reply to:

Draft