Developer forum

Forum » Ecommerce - Standard features » Send order confirmation from API

Send order confirmation from API

Martin Grønbekk Moen
Martin Grønbekk Moen
Reply

Is it possible to send and order confirmation email when creating an order using the API and not though "Shopping Cart" module?
If yes, please give examples.


Replies

 
Morten Snedker Dynamicweb Employee
Morten Snedker
Reply

Hi Martin,

You can use one of the many notification subsribers on the cart to hook up on the order and retreive the information necessary, and pass it on in a mail by using the System.Net.Mail namespace. In the example below it is the CheckoutDoneOrderIsComplete notification that is used. The example is raw data - you'd need to apply the formatting  (like html) yourself if required.

using Dynamicweb.Ecommerce.Notifications;
using Dynamicweb.Extensibility.Notifications;

namespace Ecom
{
    [Subscribe(Ecommerce.Cart.CheckoutDoneOrderIsComplete)]
    public class CheckoutDoneOrderIsComplete1 : NotificationSubscriber
    {
        public override void OnNotify(string notification, NotificationArgs args)
        {
            Ecommerce.Cart.CheckoutDoneOrderIsCompleteArgs orderArgs = args as Ecommerce.Cart.CheckoutDoneOrderIsCompleteArgs;

            string mailEmail = orderArgs.Order.CustomerEmail;
            string mailName = orderArgs.Order.CustomerName;
            string orderlines = string.Empty;

            foreach (var ol in orderArgs.Order.OrderLines)
            {
                orderlines += $"{ol.ProductName} | {ol.Quantity} | {ol.Price.PriceFormatted}<br>";
            }

            SendMail(mailEmail, mailName, orderlines);
        }

        private void SendMail(string MailAddress, string MailName, string MailBody)
        {
            // Mail stuff
        }
    }
}

Best regards
Snedker

 

 

 
Nicolai Pedersen
Reply

Remember to never use httpcontext in this piece of code, as there is none when it is a callback from a payment gateway.

 

You must be logged in to post in the forum