Developer forum

Forum » Development » Edit Subject in Order Flow Email

Edit Subject in Order Flow Email

Daniel Hollmann
Reply

Hi community.

Im trying to edit a order flow email, but I have not found a proper way to do it.

I've tried to use the subscription for BeforeSendingOrderFlow but this notifcation only contains get methods, not set methods so
it's not possible.

I've also tried to use the Ecommerce.Cart.SendingConfirmationMail but that does not get triggered when i'm sending a mail through the order flow.

There is the generic one that runs before every mail is send EmailNotifications.OnBeforeEmailSend that gets triggered, but here I don't have 
access to the order, only the email message.

Any ideas on what to do?


Replies

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Daniel,

What exactly are you trying to achieve? You are able to specify your own template for order flow emails, so depending on what you're trying to do, you may be able to implement your logic there?

- Jeppe

 
Daniel Hollmann
Reply

Hi

Thanks for the quick reply.

I have made my own template.
I'm trying to get the order country in the Email Subject, but the subject is controlled by DW Settings,
and I have to edit the subject before the mail is send, when I have a real order:

https://www.screencast.com/t/GQCLqRv4J

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Then I would recommend this approach.

Don't configure the OrderState to send to the customer. You can configure it with the template and other settings, just don't check the "Send to customer" box. Then you create a NotificationSubscriber that listens for "Dynamicweb.Ecommerce.Notifications.Order.State.Changed". Here you can do your checks and get the template for the state (order.StateId). Once you have all the bits, you can simply call one of the "Dynamicweb.Ecommerce.Services.Orders.SendEmail" overloads.

I hope that helps.

- Jeppe

 
Daniel Hollmann
Reply

Hi again, and thanks!


That certainly would be a way of doing it, however I ended implementing it in another way, properly not the easiest way but it works. Here is what I did:

The mail I had to edit the subject for was the “Others” Email, and I had to use the list of emails that was specified in the order flow.
I made a comment in the email template

 

topLeft.text.Add("<!--FieldCountry: " + GetString("Ecom:Order.Delivery.Country") + " -->");

 

And then I used the OnBeforeEmailSend subscriber to look for the comment, and edit the subject there using regex.
Here is my code for the OnBeforeEmail Send Subscriber:

 

[Subscribe(EmailNotifications.OnBeforeEmailSend)]

    public class EcomCartSendingConfirmationMailObserver : NotificationSubscriber

    {

        public override void OnNotify(string notification, NotificationArgs args)

        {

            EmailSendNotificationArgs mailingArgs = args as EmailSendNotificationArgs;

                try

                {

                    var regexForCountry = new Regex(".*<!--FieldCountry: (.*) -->.*");


                    if (regexForCountry.IsMatch(mailingArgs.Message.Body))

                    {

                        var capturedCountry = regexForCountry.Match(mailingArgs.Message.Body).Groups[1];

                        

                        if (!string.IsNullOrWhiteSpace(capturedCountry.ToString()))

                        {

                        mailingArgs.Message.Subject += $" - Country: {getCountryFromCode(capturedCountry.ToString())} ";

                        }

                    }

                    else

                    {

                        LogManager.System.GetLogger(LogCategory.Health, "Backend.OnBeforeEmailSend").Info($"Could not find Country");

                    }

                }

            catch(Exception e)

            {

                LogManager.System.GetLogger(LogCategory.Health, "Backend.OnBeforeEmailSend").Error($"Error when Finding Country in Email = {e.Message}");

            }

 

Not the cleanest way, I know. Though I would share it anyways.

 

You must be logged in to post in the forum