public bool AddProduct(IPublishedContent productPage, int quantity, decimal price) { basketItem line = lineCollection.FirstOrDefault(product => product.ProductPage.Id == productPage.Id); var exsistingProduct = false; if (line == null) { lineCollection.Add(new basketItem { ProductPage = productPage, Quantity = quantity, Price = price, IsOptionalExtra = false, IsMainProduct = true, Description = "Quantity : " + quantity + " @ : £ " + price.ToString("c"), ExtraMainProductPage = null }); // for main products check and add an non optional extras set on the product CheckAndAddExtras(productPage, quantity, price, exsistingProduct); return(true); } else if (line.Quantity > 0) { line.Quantity += quantity; line.Description = "Quantity : " + line.Quantity + " @ : £ " + price.ToString("c"); exsistingProduct = true; // for main products check and add an non optional extras set on the product CheckAndAddExtras(productPage, quantity, price, exsistingProduct); return(true); } else { return(false); } }
public bool AddExtraProduct(IPublishedContent mainProductPage, decimal mainProductPrice, IPublishedContent extraProductPage, int extraQuantity) { //var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); // check if the main product for this extra is already in the basket and set a falg to use to work out if the extra needs to be a new line or not basketItem mainProductLine = lineCollection.FirstOrDefault(product => product.ProductPage.Id == mainProductPage.Id); bool exsistingProduct = mainProductLine != null; // check if this extra is already in the basket either on the same product or another product basketItem pageExtraLine = lineCollection.FirstOrDefault(extraProduct => extraProduct.ProductPage.Id == extraProductPage.Id && extraProduct.ExtraMainProductPage.Id == mainProductPage.Id); // check if there is a price set on the extra and if its already added to the basket if (extraProductPage.GetProperty("price").HasValue) { var extraPrice = Convert.ToDecimal(extraProductPage.GetPropertyValue("price").ToString()); if (pageExtraLine == null) { AddPricedExtra(mainProductPage, extraProductPage, extraPrice, extraQuantity); return(true); } else { if (exsistingProduct) { pageExtraLine.Quantity += extraQuantity; pageExtraLine.Description = "Quantity : " + pageExtraLine.Quantity + " @ : £ " + extraPrice.ToString("c"); return(true); } else { AddPricedExtra(mainProductPage, extraProductPage, extraPrice, extraQuantity); return(true); } } } // check if it has a percentage instead of a set price and if its already added to the basket else if (extraProductPage.GetProperty("pricePercentage").HasValue) { //var mainProductPrice = Convert.ToDecimal(mainProductPage.GetPropertyValue("productPrice").ToString()); var extraPercentage = extraProductPage.GetPropertyValue("pricePercentage").ToString(); if (pageExtraLine == null) { AddPercentageExtra(mainProductPage, extraProductPage, mainProductPrice, extraPercentage, extraQuantity); return(true); } else { if (exsistingProduct) { pageExtraLine.Quantity += extraQuantity; pageExtraLine.Description = mainProductPage.Name + " , " + extraProductPage.Name + "(Quantity : " + pageExtraLine.Quantity + " ), @ : " + extraPercentage + "% of £" + mainProductPrice.ToString("c"); return(true); } else { AddPercentageExtra(mainProductPage, extraProductPage, mainProductPrice, extraPercentage, extraQuantity); return(true); } } } else { return(false); } }
//checked and add product extras public void CheckAndAddExtras(IPublishedContent productPage, int quantity, decimal price, bool exsistingProduct) { var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); //START - basket extras logic //get the extras set on the page to add to the database var productExtras = productPage.GetProperty("productExtras").Value.ToString(); if (!string.IsNullOrEmpty(productExtras)) { // check if there is more than one extra set on the page and loop through all of them if (productExtras.Contains(",")) { var extras = productExtras.Split(','); foreach (var extraId in extras) { var extraPageId = Convert.ToInt32(extraId); IPublishedContent priceExtraPage = umbracoHelper.TypedContent(extraPageId); if (priceExtraPage != null && priceExtraPage.GetProperty("isOptionalExtra").HasValue) { var isOptional = priceExtraPage.GetPropertyValue("isOptionalExtra").ToString(); if (isOptional == "False") { basketItem extraLine = lineCollection.Where(extraProduct => extraProduct.ProductPage.Id == priceExtraPage.Id).FirstOrDefault(); // check if there is a price set on the extra and if its already added to the basket if (priceExtraPage.GetProperty("price").HasValue) { var extraPrice = Convert.ToDecimal(priceExtraPage.GetPropertyValue("price").ToString()); if (extraLine == null) { AddPricedExtra(productPage, priceExtraPage, extraPrice, quantity); } else { //if this extra is already in the basket and the product has alrady been added just increase the quantity otherwise add it as a new line if (exsistingProduct) { extraLine.Quantity += quantity; extraLine.Description = "Quantity : " + extraLine.Quantity + " @ : £ " + extraPrice.ToString("c"); } else { AddPricedExtra(productPage, priceExtraPage, extraPrice, quantity); } } } // check if it has a percentage instead of a set price and if its already added to the basket else if (priceExtraPage.GetProperty("pricePercentage").HasValue) { var extraPercentage = priceExtraPage.GetPropertyValue("pricePercentage").ToString(); if (extraLine == null) { AddPercentageExtra(productPage, priceExtraPage, price, extraPercentage, quantity); } else { //if this extra is already in the basket and the product has alrady been added just increase the quantity otherwise add it as a new line if (exsistingProduct) { extraLine.Quantity += quantity; extraLine.Description = productPage.Name + " , " + priceExtraPage.Name + "(Quantity : " + extraLine.Quantity + " ), @ : " + extraPercentage + "% of £" + price.ToString("c"); } else { AddPercentageExtra(productPage, priceExtraPage, price, extraPercentage, quantity); } } } } } } } // if there is only one set then do the logic above for one item instead else { IPublishedContent priceExtraPage = umbracoHelper.TypedContent(productExtras); if (priceExtraPage != null && priceExtraPage.GetProperty("isOptionalExtra").HasValue) { var isOptional = priceExtraPage.GetPropertyValue("isOptionalExtra").ToString(); if (isOptional == "False") { basketItem extraLine = lineCollection.Where(extraProduct => extraProduct.ProductPage.Id == priceExtraPage.Id).FirstOrDefault(); // check if there is a price set on the extra and if its already added to the basket if (priceExtraPage.GetProperty("price").HasValue) { var extraPrice = Convert.ToDecimal(priceExtraPage.GetPropertyValue("price").ToString()); if (extraLine == null) { AddPricedExtra(productPage, priceExtraPage, extraPrice, quantity); } else { //if this extra is already in the basket and the product has alrady been added just increase the quantity otherwise add it as a new line if (exsistingProduct) { extraLine.Quantity += quantity; extraLine.Description = "Quantity : " + extraLine.Quantity + " @ : £ " + extraPrice.ToString("c"); } else { AddPricedExtra(productPage, priceExtraPage, extraPrice, quantity); } } } // check if it has a percentage instead of a set price and if its already added to the basket else if (priceExtraPage.GetProperty("pricePercentage").HasValue) { var extraPercentage = priceExtraPage.GetPropertyValue("pricePercentage").ToString(); if (extraLine == null) { AddPercentageExtra(productPage, priceExtraPage, price, extraPercentage, quantity); } else { //if this extra is already in the basket and the product has alrady been added just increase the quantity otherwise add it as a new line if (exsistingProduct) { extraLine.Quantity += quantity; extraLine.Description = productPage.Name + " , " + priceExtraPage.Name + "(Quantity : " + extraLine.Quantity + " ), @ : " + extraPercentage + "% of £" + price.ToString("c"); } else { AddPercentageExtra(productPage, priceExtraPage, price, extraPercentage, quantity); } } } } } } } //END - basket extras logic }