Client Pay Portal
 kentico

How to Display E-Product Links in Kentico v8.1

One of the most common customizations made to the Kentico 8 Ecommerce checkout process is to put order information on the Order Complete page. Usually this would be the invoice, order details, or some other contextual information about the purchase. Recently I needed to display the links to E-Products, which proved to be a bit tricky. I wanted to write this blog to show you how you can bring in this functionality to you code and customize the user experience.


Enabling an E-Product

In Kentico, each product has a specific “Representation”. You will need to mark a product as “E-Product” in order to get the functionality.
 


Note

You can find out more about enabling E-Products in the documentation here.


Deciding how you want to implement

There are several ways you can pull in your custom code. In this blog, I’m creating a custom web part and displaying the links. You could also have a macro, an ASPX page, or something other crazy thing to get the same results, whatever works with your project.


Accessing the Order

Regardless of the implementation method, you will need to get the order details. In my web part, I getting the last placed order for the current user within the last hour. This isn’t exact, but at the Order Complete page they are out of the purchase process and there is no contextual reference to the order. Here is the code I’m using to do that:
 
//Get the customer associated with the current user                    
CustomerInfo ci = CustomerInfoProvider.GetCustomerInfoByUserID(MembershipContext.AuthenticatedUser.UserID);
if (ci != null)
{
    //Get the latest order for the user
    DateTime dtNow = SiteContext.CurrentSite.ConvertSiteDateTime(DateTime.UtcNow);
    //Make sure it was placed in the last hour
    var orders = OrderInfoProvider.GetOrders()
            .WhereEquals("OrderCustomerID"ci.CustomerID)
            .Where("OrderDate >= DATEADD(hour, -1, '" + dtNow.ToString() + "')")
            .OrderByDescending("OrderDate")
            .TopN(1);
 
    if (orders.Count > 0)
    {
        OrderInfo oinfo = orders.FirstObject;


Getting the Download Links

This is the fun part. The code to get the links is referenced in the standard Ecommerce – Order payment notification to customer e-mail template.

 
IfDataSourceIsEmpty(EproductsTable, "","Your e-products download links were activated, please download the files before their expiration. Once the files expire, you won't be able to download them." + EproductsTable.ApplyTransformation("Ecommerce.Transformations.Order_EproductsTable"))

That code checks to see if the order contains any E-Products and generates a table using the specified transformation. This is the exact output we need in our web part / macro / etc., however, it’s pretty hidden in the API. After a good bit of spelunking, I came up with the following code that achieves the same result:
 
DataSet ds = OrderItemSKUFileInfoProvider.GetOrderItemSKUFiles(oinfo.OrderID);

The API call returns the same underlying table structure as what is in the e-mail template. Because of that, we can use the same transformation to write out our links all happy happy.


Displaying the links

Now that we have our links, we will want to display them. We certainly could loop through our DataSet and write out each thing, however, I decided to leverage some existing code / transformations to simplify the process.

In my webpart I added a CMS:QueryRepeater and then set its transformation to “Ecommerce.Transformation.Order_EproductsTable”, the same one that is used in the E-mail template.
 
<asp:Panel ID="pnlDownloadProductLinks" runat="server">
    <cms:QueryRepeater ID="qrDownloadLinks" runat="server" TransformationName="Ecommerce.Transformations.Order_EproductsTable"></cms:QueryRepeater>
</asp:Panel>

After the layout is done, all I need to do is bind my data.
 
qrDownloadLinks.DataSource = ds;
qrDownloadLinks.DataBind();

I also added a visible bool to the parent container. This web part will be on my order complete page and will be loaded for every order. This bool will allow me to only display the link sections when the order has links to display.


Note

I chose the QueryRepeater so I could specify the transformation. The Basic Repeater doesn’t give this option.


Final Result

When it's all said an done, I am now displaying my links on my Order Complete Page.
 

Here is the final layout and code behind:
 

ASPX

 
<asp:Panel ID="pnlDownloadProductLinks" runat="server">
    <cms:QueryRepeater ID="qrDownloadLinks" runat="server" TransformationName="Ecommerce.Transformations.Order_EproductsTable"></cms:QueryRepeater>
</asp:Panel>


CS
 

/// <summary>
/// Initializes the control properties.
/// </summary>
protected void SetupControl()
{
    if (this.StopProcessing)
    {
        // Do not process
    }
    else
    {
        try
        {
            //Get the customer associated with the current user                    
            CustomerInfo ci = CustomerInfoProvider.GetCustomerInfoByUserID(MembershipContext.AuthenticatedUser.UserID);
            if (ci != null)
            {
                //Get the latest order for the user
                DateTime dtNow = SiteContext.CurrentSite.ConvertSiteDateTime(DateTime.UtcNow);
                //Make sure it was placed in the last hour
                var orders = OrderInfoProvider.GetOrders()
                        .WhereEquals("OrderCustomerID"ci.CustomerID)
                        .Where("OrderDate >= DATEADD(hour, -1, '" + dtNow.ToString() + "')")
                        .OrderByDescending("OrderDate")
                        .TopN(1);
 
                if (orders.Count > 0)
                {
                    OrderInfo oinfo = orders.FirstObject;
                    //Build the return string
                    DataSet ds = OrderItemSKUFileInfoProvider.GetOrderItemSKUFiles(oinfo.OrderID);
                    if (!DataHelper.IsEmpty(ds))
                    {
                        qrDownloadLinks.DataSource = ds;
                        qrDownloadLinks.DataBind();
                        pnlDownloadProductLinks.Visible = true;
                    }
                    else
                    {
                        qrDownloadLinks.DataSource = null;
                        qrDownloadLinks.DataBind();
                        pnlDownloadProductLinks.Visible = false;
                    }
                }
                else
                {
                    qrDownloadLinks.DataSource = null;
                    qrDownloadLinks.DataBind();
                    pnlDownloadProductLinks.Visible = false;
                }
            }
        }
        catch (Exception ex)
        {
            EventLogProvider.LogException("SetupControl""EXCEPTION"ex);
            qrDownloadLinks.DataSource = null;
            qrDownloadLinks.DataBind();
            pnlDownloadProductLinks.Visible = false;
        }
    }
}

So there you go. Have fun putting your download links on every page of your site and starting some new design trend. Good luck!

Author

Wiz E. Wig, Mascot & Director of Magic
Wiz E. Wig

Director of Magic