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";
}
}