asp.netkentico

Kentico 10 OrderInfoProvider get order by guid


I am trying to get order info on the receipt page, based on the order guid from query /receipt.aspx?o=7f4e23bd-fb2d-4891-b0b2-7bbc2eaaacb5

So far I have this: (code copied from facebook pixel widget, same page)

int orderID;
var oid = Request.QueryString["o"];

Response.Write("<script language='javascript'>console.log('" + oid +"');</script>");
// this works, logs to console the order id string

if (!String.IsNullOrEmpty(oid) )
{
   int.TryParse(oid, out orderID);

   // If the text value is not a number, returns null
   if (orderID <= 0)
   {
     // 
   }
   else {
    // Gets the order based on the order ID
    OrderInfo order = OrderInfoProvider.GetOrderInfo(orderID);
                    
    Response.Write("<script language='javascript'>console.log('" + order + "');</script>");
}
}

I'm stuck at converting order GUID to int because int.TryParse(oid, out orderID) is returning 0. I am aware that it makes no sense to do this conversion


Solution

  • I confirm what was said by Cristian, you could also directly use Kentico's ValidationHelper to retrieve the Guid from the QueryString and then check if it is different from Guid.Empty. Also you cannot use the .GetOrderInfo to retrieve the order via Guid because the latter only accepts the ID (int), the correct method is this in my example.

    //var oid = Request.QueryString["o"];
    var orderGuid = ValidationHelper.GetGuid(Request.QueryString["o"], Guid.Empty);
    
    if (orderGuid != Guid.Empty)
    {
        // Gets the order based on the order Guid
        OrderInfo order = OrderInfoProvider.GetOrders()
            .WhereEquals(nameof(OrderInfo.OrderGUID), orderGuid)
            .TopN(1)
            .FirstOrDefault();
    }