More in this section
Categories
Bloggers
Blogs RSS feed

Customizing the order confirmation email in Sitefinity Ecommerce

by Venkata Koppaka

One of the popular PITS items we received was to have the ability to add custom fields to the confirmation email that is sent out after an order is placed in the Ecommerce module. Starting in Sitefinity 5.4, we have the ability to add custom fields to the email template. These fields are not restricted to just being custom fields on an order - you can add any field to the template such as  a custom field from a product or a store setting or any custom message that your store might want to embed in each email. This post details on how to add custom order fields to the order confirmation email.

Now, before I go any further please read this documentation article which explains how to add custom fields to your orders and checkout process. This article assumes that you have already added custom fields to your order and are persisting them. Your preview page should look like the screenshot below after you made necessary changes –

To get started, open the page that has your checkout widget and edit the Order Confirmation Email Template

Once you are on the template editor, paste the snippet below to add GiftMessage (a custom field that was added to the order and persisted).

Note: By convention the tokens that are replaced in the email template begin and end with “%%” and are generally all upper case.  So, in this example our custom “GiftMessage” token would be “%%GIFTMESSAGE%%”.


Now, add the following code in your Global.asax.cs file to register a custom formatter for Order Confirmation Email

using System;
using System.Linq;
using Telerik.Microsoft.Practices.Unity;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Data;
using Telerik.Sitefinity.Modules.Ecommerce.Orders.Interfaces;
  
namespace SitefinityWebApp
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            Bootstrapper.Initialized += new EventHandler<ExecutedEventArgs>(Bootstrapper_Initialized);
        }
  
        void Bootstrapper_Initialized(object sender,ExecutedEventArgs e)
        {
            if (e.CommandName == "Bootstrapped")
            {
                ObjectFactory.Container.RegisterType<IOrderConfirmationEmailTemplateFormatter, OrderConfirmationEmailTemplateFormatterWithCustomFields>(new ContainerControlledLifetimeManager());
            }
        }
   
  
        protected void Session_Start(object sender, EventArgs e)
        {
  
        }
  
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
  
        }
  
        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
  
        }
  
        protected void Application_Error(object sender, EventArgs e)
        {
  
        }
  
        protected void Session_End(object sender, EventArgs e)
        {
  
        }
  
        protected void Application_End(object sender, EventArgs e)
        {
  
        }
    }
}

Now, let’s define the OrderConfirmationEmailTemplateFormatterWithCustomFields class –

01.using System;
02.using System.ComponentModel;
03.using Telerik.Sitefinity;
04.using Telerik.Sitefinity.Ecommerce.Orders.Model;
05.using Telerik.Sitefinity.Modules.Ecommerce.Orders.Business;
06.using Telerik.Sitefinity.Modules.Ecommerce.Orders.Interfaces;
07.using Telerik.Sitefinity.Modules.Ecommerce.Orders.Web.UI.CheckoutViews;
08.  
09.namespace SitefinityWebApp
10.{
11.    public class OrderConfirmationEmailTemplateFormatterWithCustomFields : OrderConfirmationEmailTemplateFormatterBase, IOrderConfirmationEmailTemplateFormatter
12.    {
13.        public string ReplaceValuesInTemplate(string template, CheckoutState checkoutState, Order order)
14.        {
15.            string currentTemplate = ReplaceValuesInTemplateBase(template, checkoutState, order);
16.  
17.  
18.            var properties = TypeDescriptor.GetProperties(order);
19.            foreach (PropertyDescriptor property in properties)
20.            {
21.                var metaProperty = property as MetafieldPropertyDescriptor;
22.                if (metaProperty == null)
23.                    continue;
24.  
25.                if (metaProperty.Name == "GiftMessage")
26.                {
27.                    object value = metaProperty.GetValue(order);
28.                    if (value != null)
29.                    {
30.                        currentTemplate = currentTemplate.Replace("%%GIFTMESSAGE%%", value.ToString());
31.                    }
32.                    else
33.                    {
34.                        currentTemplate = currentTemplate.Replace("%%GIFTMESSAGE%%", "N/A");
35.                    }
36.                }
37.            }
38.            return currentTemplate;
39.        }
40.    }
41.}

Please note in the code above, inheriting from OrderConfirmationEmailTemplateFormatterBase is not mandatory. The class gives you a bunch of “helper” methods to make the formatting logic easier for you, the developer.

ReplaceTemplateValuesInTemplateBase method (part of OrderConfirmationEmailTemplateFormatterBase) returns a string notation of the formatted template which can be further modified, Lines 18 – 37 does exactly that and adds the GiftMessage string to the email.

After using the code the email now looks like below –

Happy Coding!

14 comments

Leave a comment
  1. Pierre Alain Carrier Mar 08, 2013
    Is there documentation on the fields that can be used out-of-the-box with this syntax?
  2. Svetla Yankova Apr 12, 2013
    Hi Pierre,
    We'll add this to the official docs as soon as possible, but the list I have is as following. There might be a few new ones that we need to add, as this is pre-Sitefinity 5.4. Hope this helps!

    %%STORENAME%%

    %%STOREURL%%

    %%SHIPPINGMETHOD%%

    %%TOTALQUANTITY%%

    %%SUBTOTALDISPLAY%%

    %%SHIPPINGTOTAL%%

    %%TAX%%

    %%TOTAL%%

    %%/IFSHIPPABLE%%

    %%IFSHIPPABLE%%

    %%DISCOUNTS%%

    %%DISCOUNTTITLE%%

    %%DISCOUNTSAVINGSAMOUNT%%

    %%DISCOUNTS%%

    %%PRODUCTS%%

    %%PRODUCTSKU%%

    %%PRODUCTTITLE%%

    %%PRODUCTOPTIONS%%

    %%PRODUCTPRICE%%

    %%PRODUCTQUANTITY%%

    %%DETAILTOTAL%%

    %%CREDITCARDTYPE%%

    %%CREDITCARDNUMBER%%

    %%CREDITCARDTYPE%%

    %%CREDITCARDNUMBER%%

    %%BILLINGADDRESS1%%

    %%BILLINGADDRESS2%%

    %%BILLINGCITY%%

    %%BILLINGCOMPANYNAME%%

    %%BILLINGCOUNTRY%%

    %%BILLINGEMAIL%%

    %%BILLINGFIRSTNAME%%

    %%BILLINGLASTNAME%%

    %%BILLINGPHONE%%

    %%BILLINGSTATE%%

    %%BILLINGPOSTAL%%

    %%SHIPPINGADDRESS1%%

    %%SHIPPINGADDRESS2%%

    %%SHIPPINGCITY%%

    %%SHIPPINGCOMPANYNAME%%

    %%SHIPPINGCOUNTRY%%

    %%SHIPPINGEMAIL%%

    %%SHIPPINGFIRSTNAME%%

    %%SHIPPINGLASTNAME%%

    %%SHIPPINGPHONE%%

    %%SHIPPINGSTATE%%

    %%SHIPPINGPOSTAL%%

    %%ORDERNUMBER%%

    %%PLACEDON%%
  3. Surbhi Sep 24, 2013
    Hi,
    How do I get the existing variables in code behind?
    I want to replace the %%productprice%% variable value to some other value which would be based on some calculations.

    Please help!

    Thanks,
    Surbhi Agarwal
  4. James Oct 24, 2013
    How do I show an Item's custom field in the email?
  5. Dominic Apr 15, 2016

    My variable %%BILLINGLASTNAME%% is not showing the complete last name of the user.

    The last name should be : "Lecours Brewster" and it only shows "Lecours".  It seems like the last name is truncated on the space.


    How can I fix that problem 

  6. Robinjack Nov 05, 2017
    thank you for a great post. escorts lebanon
  7. Robinjack Nov 09, 2017
    I am very much pleased with the contents you have mentioned. I wanted to thank you for this great article. beirut escort
  8. Robinjack Nov 14, 2017
    You have done a great job on this article.  It’s very readable and highly intelligent.  You have even managed to make it understandable and easy to read.  You have some real writing talent. Thank you. Fennel Essential Oil
  9. Robinjack Nov 18, 2017
    The post is composed in exceptionally a decent way and it contains numerous valuable data for me. Hero Instinct
  10. Robinjack Nov 23, 2017
    I truly thank you for the important information on this extraordinary subject and anticipate more awesome posts. Much obliged for appreciating this excellence article with me. I am welcoming it all that much! Anticipating another awesome article. Good fortunes to the creator! All the best!  His Secret Obsession
  11. Robinjack Nov 27, 2017
    Affection to peruse it,Waiting For More new Update and I Already Read your Recent Post its Great Thanks.  lifestyle public relations
  12. Robinjack Nov 30, 2017
    Decent Informative Blog having pleasant sharing..  Natural Food
  13. Robinjack Dec 03, 2017
    You have a genuine capacity for composing one of a kind substance. I like how you think and the way you speak to your perspectives in this article. I concur with your mindset. Much obliged to you for sharing.  apply for esta
  14. robinjack Dec 03, 2017
    A debt of gratitude is in order for the decent blog. It was extremely helpful for me. I'm glad I discovered this site. Much thanks to you for offering to us,I too dependably discover some new information from your post.  istruzioni schneider electric

    Leave a comment