Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > Ecommerce: Supplier Email View modes: 
User avatar
Member
Member
egarrison-wte - 10/15/2013 9:47:35 AM
   
Ecommerce: Supplier Email
RE: V7 Ecommerce

I have suppliers setup on the products. Is there a way to have them get an automated email when and order is placed with the items tied to them? The suppliers have email address populated, but it doesn't look like there is any template or process to send to them. Ecommerce guide is lacking the info on this.

User avatar
Certified Developer 10
Certified Developer 10
josha-bpstudios - 10/16/2013 3:13:12 PM
   
RE:Ecommerce: Supplier Email
Here is an example run as a scheduled task. Basically this gets all of the orders made in the last hour and loops through the order items in each order. I haven't tested this or anything, but it should at least get you started or on the right track. This method will send an email per item...which may need to be modified on your behalf to get all of the items where the manufacturer=a and send an email for the order, then get all items where the manufacturer =b and send an email for the same order.
 public class CustomTaskOrderEmails : ITask
{
public string Execute(TaskInfo ti)
{

//bring in orders and loop through getting recent orders...
string sWhere = string.Empty;
string sOrderBy = string.Empty;
string emailBody = string.Empty;
SupplierInfo si = null;

sWhere = "OrderDate >= " + "'" + DateTime.Now.AddHours(-1) + "'";
sOrderBy = "OrderDate ASC";

DataSet dsNewOrders = OrderInfoProvider.GetOrders(sWhere, sOrderBy, 200, null, CMSContext.CurrentSiteID);
if (DataHelper.IsEmpty(dsNewOrders) && dsNewOrders.Tables[0].Rows.Count > 0)
{
return "No New Orders";
}

foreach (DataRow row in dsNewOrders.Tables[0].Rows)
{
//TODO set up email template for emails to be sent out.
CustomerInfo ci = CustomerInfoProvider.GetCustomerInfo(Convert.ToInt32(row["OrderCustomerID"]));
DataSet dsOrderItems = OrderItemInfoProvider.GetOrderItems(Convert.ToInt32(row["OrderID"]));
foreach (DataRow orderItem in dsOrderItems.Tables[0].Rows)
{
if (ci != null)
{
OrderInfo oi = OrderInfoProvider.GetOrderInfo(Convert.ToInt32(orderItem["OrderItemOrderID"]));
if (oi.OrderIsPaid)
{
SKUInfo sku = SKUInfoProvider.GetSKUInfo(Convert.ToInt32(orderItem["OrderItemSKUID"]));
si = SupplierInfoProvider.GetSupplierInfo(sku.SKUSupplierID);
CMS.EmailEngine.EmailTemplateInfo ei = null;
if (si.SupplierEmail == "alexJ@greentechenv.com")
{
ei = EmailTemplateProvider.GetEmailTemplate("GreenTechOrderItemDetails", CMSContext.CurrentSiteID);
}
else if (si.SupplierEmail == "info@nationalenergytech.com")
{
ei = EmailTemplateProvider.GetEmailTemplate("NETOrderItemDetails", CMSContext.CurrentSiteID);
}
else
{
//ei will still be null if neither of the cases above goes through.
return null;
}
if (ei != null)
{
EmailMessage message = new EmailMessage();
message.Body = ei.TemplateText;
message.EmailFormat = EmailFormatEnum.Default;
if (!string.IsNullOrEmpty(ei.TemplateFrom))
{
message.From = ei.TemplateFrom;
}
else
{
message.From = ci.CustomerEmail;
}
message.Recipients = si.SupplierEmail;

string[,] macros = new string[9, 2];
macros[0, 0] = "CustomerFullName";
macros[0, 1] = ci.CustomerFirstName + " " + ci.CustomerLastName;
macros[1, 0] = "OrderDate";
macros[1, 1] = row["OrderDate"].ToString();
macros[2, 0] = "OrderID";
macros[2, 1] = orderItem["OrderItemOrderID"].ToString();
macros[3, 0] = "SKUName";
macros[3, 1] = orderItem["OrderItemSKUName"].ToString();
macros[4, 0] = "SKUPrice";
macros[4, 1] = orderItem["OrderItemTotalPriceInMainCurrency"].ToString();
macros[5, 0] = "CustomerAddress";
AddressInfo ai = AddressInfoProvider.GetAddressInfo(oi.OrderShippingAddressID);
//TODO Insert address line 2 : (!string.IsNullOrEmpty(ai.AddressLine2.ToString())) ? ai.AddressLine2 : string.Empty +
macros[5, 1] = ci.CustomerFirstName + " " + ci.CustomerLastName + "<br/>" + " " + ai.AddressLine1 + "<br/>" + ai.AddressCity + ", " + StateInfoProvider.GetStateInfo(ai.AddressStateID).StateDisplayName + " " + ai.AddressZip + "<br/>" + ai.AddressPhone;
macros[6, 0] = "OrderPaymentOption";
macros[6, 1] = PaymentOptionInfoProvider.GetPaymentOptionInfo(oi.OrderPaymentOptionID).PaymentOptionDisplayName;
macros[7, 0] = "OrderShippingOption";
macros[7, 1] = ShippingOptionInfoProvider.GetShippingOptionInfo(oi.OrderShippingOptionID).ShippingOptionDisplayName;
macros[8, 0] = "OrderNote";
macros[8, 1] = oi.OrderNote;

MacroResolver macroResolve = MacroResolver.GetInstance();
macroResolve.SourceParameters = macros;

try
{
macroResolve.EncodeResolvedValues = false;
macroResolve.EncodeSpecialMacros = false;
string body = URLHelper.MakeLinksAbsolute(ei.TemplateText);
message.Body = macroResolve.ResolveMacros(body);
EmailSender.SendEmail(CMSContext.CurrentSiteName, message, true);
}
catch (Exception ex)
{
return "Failed";
}
}

}
//if ORDERISPAID = false, then don't send the emails...
}
}

}
string detail = "Executed from '~/App_Code/KenticoTraining/CustomTaskOrderEmails.cs'. Task Data:";
EventLogProvider.LogInformation("CustomtaskOrderEmails", "Execute", detail);

return "Success";
}
}

User avatar
Kentico Support
Kentico Support
kentico_zdenekc - 10/22/2013 9:06:36 AM
   
RE:Ecommerce: Supplier Email
Hi,

Thanks a lot to josha for the comprehensive post!

To add a little idea - if the aim is to send the email as soon as possible after placing an order, you could use global event handler, which would detect when the Order object is created or updated and fire the email sending action.
More details about event handling can be found at http://devnet.kentico.com/docs/7_0/devguide/event_handlers_overview.htm

Please feel free to contact us if you have any additional questions.

Regards,
Zdenek

User avatar
Member
Member
egarrison-wte - 10/22/2013 7:42:11 PM
   
RE:Ecommerce: Supplier Email
Thanks. I ended up using Josh's idea. I did also notice that I could have used a Database trigger and write to the email table to send out.

User avatar
Certified Developer 10
Certified Developer 10
josha-bpstudios - 10/23/2013 12:55:06 PM
   
RE:Ecommerce: Supplier Email
What did you find out about the database trigger option? I have never messed around with them, but I would be interested to know more about it.

Thanks,
Josh

User avatar
Member
Member
egarrison-wte - 10/24/2013 4:26:58 PM
   
RE:Ecommerce: Supplier Email
I have used them a few times. I will post back up here a trigger to do this. It will send email based on order items to suppliers. I have enhanced the system in a few areas with triggers.