Table of Contents

Delete orderlines on Line.Removed notification

How to delete other orderlines on a Line.Removed notification

Orderline.Removed notification

A Line.Removed notification is triggered whenever an orderline is removed from the cart. From time to time, when this happens, you may want to evaluate the order and/or the other orderlines, and if a given rule is being honored, then other orderlines should be removed as well.

However, it is important to notice that the Order object has all orderlines, including the one that has been deleted (causing the Line.Removed notification). So if you want to delete further orderlines, it is important that you do not delete them through your iteration, but rather add them to a list, and delete them separately. Otherwise you may very well run into a 'InvalidOperationException' because the orderlines you are iterating through, is invalidated (changed outside of scope).

In the example below we have orderlines with a value set in ParentId. However, the custom part is that these orderlines have OrderLineType 'Fixed', rather than Discount/ProductDiscount. This also means that the native functionality of the Dynamicweb API, where child orderlines are automatically deleted when their parent is deleted, is not triggered. So any child orderlines we need to delete manually.

Worth noticing:

  1. Other lines that we want to delete (lines with value in ParentId) are added to a List object, removeList.
  2. The use of SuppressNotifications. When deleting additional orderlines in list removeList it is wrapped in a 'using (NotificationContext.SuppressNotifications)'. This will ensure that the Line.Removed notification is not triggered when additional orderlines are removed. Whether or not suppressing further notifications makes sense, should be evaluated from case to case. Just have in mind that if suppressing not used then you will have a notification for every line being deleted. Though, in this particular scenario it is useful.
using Dynamicweb.Ecommerce.Notifications;
using Dynamicweb.Extensibility.Notifications;
using System.Collections.Generic;

namespace MyCustom.NotificationSubscribers
{
    /// <summary>
    /// Ensure that discount product lines are removed, if parent line is deleted.
    /// As the discount lines are actually of OrderLineType 'Fixed', they will not be deleted automatically along with the parent.
    /// </summary>
    [Subscribe(Ecommerce.Cart.Line.Removed)]
    public class LineRemoved1 : NotificationSubscriber
    {
        public override void OnNotify(string notification, NotificationArgs args)
        {
            if (!(args is Ecommerce.Cart.Line.RemovedArgs))
                return;

            // If this notification is due to cart being emptied, ignore notification and exit, since all lines are removed.
            var cmd = Dynamicweb.Context.Current.Request["cartcmd"];
            if (cmd != null && cmd == "emptycart")
            {
                return;
            }

            Ecommerce.Cart.Line.RemovedArgs removed = (Ecommerce.Cart.Line.RemovedArgs)args;
            List<Dynamicweb.Ecommerce.Orders.OrderLine> removeList = new List<Dynamicweb.Ecommerce.Orders.OrderLine>();
            string removedId = removed.RemovedLine.Id; // the Id of current OrderLine inflicted by Line.Removed notification.

            // List of OrderLines to be removed: lines that in ParentLineId have the same value as that of the RemovedLine.Id.
            foreach (var line in removed.Cart.OrderLines)
            {
                if (line.ParentLineId == removedId && line.Id != removedId)
                {
                    // This is a child line: add to list of OrderLines we wish to remove.
                    removeList.Add(line);
                }
            }

            // Now, delete child-orderlines by List. Suppress further Line.Removed notifications.
            foreach (var line in removeList)
            {
                using (NotificationContext.SuppressNotifications)
                {
                    removed.Cart.OrderLines.Remove(line);
                }
            }
        }
    }
}
To top