NotificationSubscribers - Part 5 - Changing the Order ID
Here's a quick code example that shows you how to modify the ID of an order in the database after the order has been completed. This can be useful if you need to modify the ID to include the language for example. Note that this code executed when the order has been marked as complete. If you need to do this earlier, you can also override the method OnBeforeRenderStep5 of your Gateway Provider. For more information, check out this thread on the Engage forum.
The following code snippet changes the order ID by prefixing it with NL or DK based on the LanguageID property of the order.
[Subscribe(Dynamicweb.Notifications.eCommerce.Order.Steps.Completed)] public class CheckOutDoneOrderIsComplete : NotificationSubscriber { public override void OnNotify(string notification, object[] args) { Order order = args[0] as Order; if (order == null) { throw new ArgumentException(@"Order is null in CheckOutDoneOrderIsComplete.OnNotify."); }
string orderIdPrefix; switch (order.LanguageID) { case "LANG1": orderIdPrefix = "NL"; break; case "LANG2": orderIdPrefix = "DK"; break; default: throw new Exception("Unsupported Order Language"); } order.ChangeOrderId(orderIdPrefix + order.ID); order.Save(); } }
This code uses an Extension method called ChangeOrderId that looks as follows:
public static class OrderExtensions { /// <summary> /// Changes the ID of an existing order to the one /// specified in <paramref name="newId"/>. /// </summary> /// <param name="order">The order being extended.</param> /// <param name="newId">The new ID of the order.</param> public static void ChangeOrderId(this Order order, string newId) { if (order == null) { throw new ArgumentNullException("order", "order is null."); } if (String.IsNullOrEmpty(newId)) { throw new ArgumentException("newId is null or empty.", "newId"); } string sql = ""; sql = String.Format("UPDATE EcomOrders SET OrderID = '{0}', OrderCart = {1} " + "WHERE OrderID = '{2}' ", newId, Database.SqlBool(false), order.ID); Database.ExecuteNonQuery(sql, "Ecom.mdb"); sql = String.Format("UPDATE EcomOrderLines SET OrderLineOrderID = '{0}' " + WHERE OrderLineOrderID = '{1}' ", newId, order.ID); Database.ExecuteNonQuery(sql, "Ecom.mdb"); order.ID = newId; } }