Hello,
I have a solution where some products can only be added to the basket in multiple (e.g if 6 then only 6,12, 18, etc). I have subscribed to Line.Decreased and Line.Increased, and it works fine except for one detail : when I decrease at minimum quantity, the line stays in the basket with a quantity of one. I can't manage to simple delete the line.
ex : I have a product with a 6 multiple, and I have 6 in the cart. If I decrease, the quantity should go to 0, and the line be deleted. However this doesn't happen, I simply get a quantity of one. If I decrease again, the line is finally deleted.
Here's my code :
public class OnDecreased : Dynamicweb.Extensibility.Notifications.NotificationSubscriber
{
public override void OnNotify(string notification, Dynamicweb.Extensibility.Notifications.NotificationArgs args)
{
if (args == null || !(args is Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.Line.DecreasedArgs))
return;
Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.Line.DecreasedArgs item = (Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.Line.DecreasedArgs)args;
var product = item.DecreasedLine.Product;
if (MultipleUtils.IsProductMultiple(product))
{
// Does return 0 if quantity goes below multiple
var newQuantity = MultipleUtils.GetLowerMultipleQuantity(item.DecreasedLine.Quantity, product);
item.DecreasedLine.Quantity = newQuantity;
item.DecreasedLine.Save();
//Tried this, doesn't work
//if (newQuantity == 0)
//{
// item.DecreasedLine.Delete();
// item.Cart.Save();
//}
}
}
}
Thanks