Dynamicweb.eCommerce.Orders Namespace > OrderLine Class > Delete Method : Delete() Method |
class MyPage : System.Web.UI.Page { private Order order; public void RemoveNoneActiveProducts() { //HACK: This prevents dbProduct.Price.Price below to call this method and create an endless loop if (order.IsCart) { Context.SetCart(order); } bool doCheckStock = Base.ChkBoolean(Base.GetGs("/Globalsettings/Ecom/Product/DontShowProductIfNotOnStock")); bool doCheckPrice = Base.ChkBoolean(Base.GetGs("/Globalsettings/Ecom/Product/DontShowProductIfHasNoPrice")); //First build a list of ID's of the products to get from the database List<string> ids = new List<string>(); List<OrderLine> productLines = new List<OrderLine>(); foreach (OrderLine orderLine in order.OrderLines) { if (orderLine.Type == Base.ChkString(Base.ChkInteger(orderLine.OrderLineType.Product))) { ids.Add(orderLine.Product.ID); productLines.Add(orderLine); } } //Get the products that match the ids ProductCollection productsFromDatabase = Product.getProducts(ids.ToArray(), false); //Remove the products that don't exist or is not active foreach (OrderLine productLine in productLines) { bool doRemove = true; Product product = productLine.Product; foreach (Product dbProduct in productsFromDatabase) { if (product.ID == dbProduct.ID && product.VariantID == dbProduct.VariantID && product.LanguageID == dbProduct.LanguageID) { if (dbProduct.Active) { doRemove = false; } if (! doRemove && doCheckPrice && dbProduct.Price.Price <= 0) { doRemove = true; } if (! doRemove && doCheckStock && dbProduct.UnitStock <= 0) { doRemove = true; } break; } } if (doRemove) { order.OrderLines.Remove(productLine); productLine.Delete(); } } } }
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2