Page 1 of 1
Customize trigger
Posted: Thu Jun 11, 2009 11:49 am
by Atiq
Hello,
I want to add a customize trigger. Is there any option to add it?
If not, please give me some idea how can I send mail to a certain email address after buying certain product ?
Regards
Atiq
Re: Customize trigger
Posted: Thu Jun 11, 2009 1:10 pm
by Logan Rhodehamel
You could configure a vendor record and set the email address. Then associate the product with the vendor. Then create an email associated with the order placed event, using vendor as the to address.
This will only work if you are not making use of vendors - in other words this is the only vendor in your system. Otherwise the email you configure will always be sent to vendors and that is probably not what you want.
Re: Customize trigger
Posted: Thu Jun 11, 2009 5:06 pm
by ryanstowasser
You could use warehouses, but it takes some additional code.
We set up something similar that used warehouses for one of our customers.
Some code runs after each purchase that sends out an email to the warehouses included in the order if they have an email address related to it.
Re: Customize trigger
Posted: Thu Jun 11, 2009 5:25 pm
by Atiq
Thanks Logan_AbleCommerce & ryanstowasser.
Logan_AbleCommerce, the solution you provide only works for one vendor. But in my system there are more than one product for which email should be send. For different product there should be different email address. After purchasing the product an message will send to the email address assign for it.
Please provide some advice on how to accomplish the above
ryanstowasser, can you please share the code & the idea more elaborately?
Thanks
Atiq
Re: Customize trigger
Posted: Fri Jun 12, 2009 5:21 am
by mazhar
AbleCommerce Email system supports vendor keyword which means system will automatically send an Email to all available vendors within that order. Update your Email template and put word vendor in To address text box.
Re: Customize trigger
Posted: Fri Jun 12, 2009 7:11 am
by Atiq
Thanks mazhar.
It seems that I can't explain my problem correctly. I explain it with an example below.
There are five products. ProductA, ProductB, ProductC, ProductD, ProductE.
After purchasing ProductA, a message with subject "thanks to buy our product" & body "It is a good decision" will send to "
abc@abc.com"
After purchasing ProductB, a message with subject "You are winner" & body "You take the right decision" will send to "
xyz@xyz.com"
After purchasing ProductC, a message with subject "It is profitable" & body "You are lucky" will send to "
pqs@pqs.com"
After puchasing ProductD & ProductE no message will send.
Please notice that message content ,subject, toAddress are different for different product.
Thanks
Atiq
Re: Customize trigger
Posted: Fri Jun 12, 2009 7:23 am
by mazhar
In this case you need to manually trigger Email. Please read following thread about how to trigger Emails manually.
viewtopic.php?f=42&t=8571
In your case you can put code for Email trigger in OnePageCheckout.ascx.cs class's
CheckedOut method, where you can get current order by using following code
Code: Select all
Order order = OrderDataSource.Load(e.OrderId,false);
viewtopic.php?f=42&t=8682
Re: Customize trigger
Posted: Fri Jun 12, 2009 7:58 am
by ZLA
Also review this post which is also on point:
viewtopic.php?f=42&t=8571&hilit=+approved I customized our site to send vendor notification emails to multiple vendors when the order included products from different vendors.
Instead of
Code: Select all
EmailTemplateCollection emailTemplates = EmailTemplateDataSource.LoadForCriteria(" Name = 'VendorNotificationEx' ");
you would need to load a different template for each vendor. I would suggest you add your own table xyz_VendorEmailTemplates to make this easily manageable. Something like:
- VendorId
- EmailTemplateId (or EmailTemplateName)
I needed to add the "trigger" code in two places:
Admin/Orders/ViewOrder.aspx.cs - OrderActionButton_Click:
Code: Select all
if (action.StartsWith("OS_"))
{
//UPDATE ORDER STATUS REQUESTED
int orderStatusId = AlwaysConvert.ToInt(action.Substring(3));
//VALIDATE STATUS
OrderStatus status = OrderStatusDataSource.Load(orderStatusId);
if (status != null)
{
// SG Customization - ZLA 2009-06-10 - separate vendor emails
if (status.Name == "Shipment Approved")
{
SG_OrderHelper sgOrder = new SG_OrderHelper(_Order);
sgOrder.SendVendorNotifications();
}
// SG End Customization - ZLA 2009-06-10 - separate vendor emails
_Order.OrderStatusId = status.OrderStatusId;
_Order.Save();
}
BindOrderActions();
}
Admin/Orders/Default.aspx.cs - BatchButton_Click:
Code: Select all
if (BatchAction.SelectedValue.StartsWith("OS_"))
{
//UPDATE ORDER STATUS REQUESTED
int orderStatusId = AlwaysConvert.ToInt(BatchAction.SelectedValue.Substring(3));
//VALIDATE STATUS
OrderStatus status = OrderStatusDataSource.Load(orderStatusId);
if (status != null)
{
foreach (int orderId in orderIds)
{
Order order = OrderDataSource.Load(orderId);
if (order != null)
{
// SG Customization - ZLA 2009-06-10 - separate vendor emails
if (status.Name == "Shipment Approved")
{
SG_OrderHelper sgOrder = new SG_OrderHelper(order);
sgOrder.SendVendorNotifications();
}
// SG End Customization - ZLA 2009-06-10 - separate vendor emails
order.UpdateOrderStatus(status);
}
}
}
}
They both call a custom class that does the emailing:
Code: Select all
/// <summary>
/// Custom Application Code related to Orders
/// </summary>
public class SG_OrderHelper
{
private Order _Order;
public SG_OrderHelper(Order order)
{
_Order = order;
}
/// <summary>
/// Sends email to each vendor in an order. Email Template is responsible for including just that vendor's items.
/// </summary>
public void SendVendorNotifications()
{
VendorCollection vendors = new VendorCollection();
foreach (OrderItem orderItem in _Order.Items)
{
if (orderItem.OrderItemType == OrderItemType.Product)
if (orderItem.Product.Vendor != null && !vendors.Contains(orderItem.Product.Vendor))
vendors.Add(orderItem.Product.Vendor);
}
foreach (Vendor vendor in vendors)
{
EmailTemplateCollection emailTemplates = EmailTemplateDataSource.LoadForCriteria(" Name = 'SG Vendor Notification' ");
if (emailTemplates.Count > 0)
{
emailTemplates[0].Parameters.Add("store", Token.Instance.Store);
emailTemplates[0].Parameters.Add("order", _Order);
emailTemplates[0].Parameters.Add("vendorname", vendor.Name);
emailTemplates[0].ToAddress = vendor.Email;
emailTemplates[0].Send();
}
}
}
...
Also note that you will need to modify your email templates so they only show that vendor's products. By default, they would include all products in the order. Thus you'll need to change
Code: Select all
#foreach( $orderItem in $order.Items )
#if ($orderItem.OrderShipmentId == $shipment.OrderShipmentId)
to
Code: Select all
#foreach( $orderItem in $order.Items )
#if($orderItem.Product.Vendor.Name == $vendorname)
#if ($orderItem.OrderShipmentId == $shipment.OrderShipmentId)
(Don't forget to add the matching #end)
Regards.
Re: Customize trigger
Posted: Mon Jun 15, 2009 3:45 pm
by Atiq
Thanks a lot ZLA & mazhar.
I solve my problem.
But it takes long time to send the mail. Sometimes I receive mail after 9/10 hrs. Sometimes two or three days later.
The EmilTemplates name are same as vendor in my shopping cart. So, instead of table I find the EmailTemplates by the vendor name.
I edit my Admin/Orders/Payments/CapturePayment.aspx.cs like below.
Code: Select all
protected void SubmitCaptureButton_Click(object sender, EventArgs e)
{
//GET THE CAPTURE AMOUNT
LSDecimal captureAmount = AlwaysConvert.ToDecimal(CaptureAmount.Text);
bool finalCapture = NoAdditionalCapture.Checked;
if (captureAmount > 0)
{
_Payment.Capture(captureAmount, finalCapture, false);
if (!string.IsNullOrEmpty(CustomerNote.Text))
{
OrderNote note = new OrderNote(_Order.OrderId, Token.Instance.UserId, DateTime.UtcNow, CustomerNote.Text, NoteType.Public);
note.Save();
}
}
//Vendor Notification Code Start
foreach (OrderItem orderItem in _Order.Items)
{
if (orderItem.OrderItemType == OrderItemType.Product)
{
if (orderItem.Product.Vendor != null)
{
EmailTemplateCollection emailTemplates = EmailTemplateDataSource.LoadForCriteria(" Name = '"+orderItem.Product.Vendor.Name+"' ");
if (emailTemplates.Count > 0)
{
emailTemplates[0].Parameters.Add("store", Token.Instance.Store);
emailTemplates[0].Parameters.Add("order", _Order);
emailTemplates[0].Parameters.Add("vendorname", orderItem.Product.Vendor.Name);
emailTemplates[0].ToAddress = orderItem.Product.Vendor.Email;
emailTemplates[0].Send();
}
}
}
}
//Vendor Notification Code End
Response.Redirect("Default.aspx?OrderId=" + _OrderId.ToString());
}
Can anyone please give solution to send the message instantly?
Regards
Atiq
Re: Customize trigger
Posted: Mon Jun 15, 2009 3:57 pm
by ZLA
The Send() method command sends the message immediately. The delay you're seeing must be related to your ISP or other internet issues. Unfortunately, I'm not able to suggest much more than that.
Re: Customize trigger
Posted: Mon Jun 15, 2009 4:35 pm
by Atiq
Thanks again.
Now I want to send email for the kit components just like product.
What changes need in my code?
Re: Customize trigger
Posted: Tue Jun 16, 2009 4:11 am
by mazhar
You can process kit components something like following code
Code: Select all
Product product = ProductDataSource.Load(productId);
foreach (ProductKitComponent pkc in product.ProductKitComponents)
{
foreach (KitProduct choice in component.KitProducts)
{
choice.Product;//Process Here
}
}
Re: Customize trigger
Posted: Wed Jun 17, 2009 2:06 pm
by Intelliflex
Mazhar
I was working with the code you had put up, and the issue I was having during testing was when a vendor had more than one product in the order, they would get multiple emails (times the number of products in the order) for the same order. I have modifed the code to not add Vendors that already exist in the Collection and in initial testing appears to do the trick.
Code: Select all
VendorCollection vendors = new VendorCollection();
foreach (OrderItem orderItem in _Order.Items)
{
if (orderItem.OrderItemType == OrderItemType.Product)
if (orderItem.Product.Vendor != null)
{
if (!vendors.Contains(orderItem.Product.Vendor))
{
vendors.Add(orderItem.Product.Vendor);
}
}
}
foreach (Vendor vendor in vendors)
{
EmailTemplateCollection emailTemplates = EmailTemplateDataSource.LoadForCriteria(" Name = 'VendorNotificationEx' ");
if (emailTemplates.Count > 0)
{
emailTemplates[0].Parameters.Add("store", Token.Instance.Store);
emailTemplates[0].Parameters.Add("order", _Order);
emailTemplates[0].Parameters.Add("vendorname",vendor.Name);
emailTemplates[0].ToAddress = vendor.Email;
emailTemplates[0].Send();
}
}
Re: Customize trigger
Posted: Wed Jun 17, 2009 2:31 pm
by ZLA
You may want to review the link I posted on the 12th. It correctly handles multiple items from the same vendor. It also addresses an order with multiple vendors' products so you don't send one vendor's items to another.
Regards.
Re: Customize trigger
Posted: Wed Jun 17, 2009 2:41 pm
by Intelliflex
Mazhar
Just went back and found it, not sure how I missed that one, I went through them several times to make sure I was getting the latest code.
I think I was working off another post thread, and when I wanted to post the fix, I did a search and pulled this thread up.
Thanks for all your help!