Developer forum

Forum » Development » Cart.Line.Increased triggers itself, runs twice.

Cart.Line.Increased triggers itself, runs twice.

August Schnell
Reply

I'm having an issue where my Cart.Line.Increased code runs twice, this only happens when i change the quantity of the IncreasedLine.

Changing item.IncreasedLine.Quantity triggers the subscriber again.

So if I increment my item.IncreasedLine by 1, otherOrderline.Quantity gets increased by 2 instead of 1. 
Also for some reason i do not understand, this code does not run in a loop, but only twice. 

I'm running DW version 9.16.7

//find another orderline with the same product number but different variant code, and add the remaining quantity to that orderline
var otherOrderline = item.IncreasedLine.Order.OrderLines.FirstOrDefault(ol =>
    ol.ProductNumber == productNumber && ol.ProductVariantId != variantCode);

if (otherOrderline != null)
{
    
    //removing this, makes the code stop running twice
    item.IncreasedLine.Quantity = matchingPriceLine.QuantityInStock;

    otherOrderline.Quantity += item.AmountIncreased;    
    
    
    
}

Replies

 
Morten Snedker Dynamicweb Employee
Morten Snedker
Reply

Hi August,

You may want to consider the use of

using (NotificationContext.SuppressNotifications)

like in this example.

Suppressing will prevent a cyclic notification.

BR
Snedker

 
August Schnell
Reply

Hi Morten

I'm still having the same issue after using SuppressNotifications. I ended up doing this instead:

//Using context to avoid double increment
if (Dynamicweb.Context.Current.Items["increment"] == "true")
{
    otherOrderline.Quantity += item.AmountIncreased;
    Dynamicweb.Context.Current.Items["increment"] = "false";
}

item.IncreasedLine.Quantity = matchingPriceLine.QuantityInStock;
Dynamicweb.Context.Current.Items["increment"] = "true";

 

You must be logged in to post in the forum