public ActionResult AddToCart(ShirtVariation currentContent, decimal Quantity, string Monogram) { // ToDo: (lab D1) add a LineItem to the Cart var cart = _orderRepository.LoadOrCreateCart <ICart>(PrincipalInfo.CurrentPrincipal.GetContactId(), "Default"); var cartItem = cart.GetAllLineItems().SingleOrDefault(item => item.Code == currentContent.Code); if (cartItem == null) { cartItem = _orderGroupFactory.CreateLineItem(currentContent.Code, cart); cartItem.Quantity = Quantity; cart.AddLineItem(cartItem); } else { cartItem.Quantity += Quantity; } var validLineItem = _lineItemValidator.Validate(cartItem, cart.MarketId, (item, issue) => { }); if (validLineItem) { cartItem.Properties["Monogram"] = Monogram; _orderRepository.Save(cart); } // if we want to redirect ContentReference cartRef = _contentLoader.Get <StartPage>(ContentReference.StartPage).Settings.cartPage; CartPage cartPage = _contentLoader.Get <CartPage>(cartRef); var name = cartPage.Name; var lang = ContentLanguage.PreferredCulture; string passingValue = cart.Name; // go to the cart page, if needed return(RedirectToAction("Index", lang + "/" + name, new { passedAlong = passingValue })); }
private ILineItem GetOrAdjustLineItem(ICart cart, string code, decimal quantity, string monogram) { var item = cart.GetAllLineItems() .FirstOrDefault(x => x.Code.Equals(code, StringComparison.OrdinalIgnoreCase)); if (item != null) { item.Quantity += quantity; } else { item = _orderGroupFactory.CreateLineItem(code, cart); item.Quantity = quantity; if (_lineItemValidator.Validate(item, cart.Market, (i, issue) => { })) { cart.AddLineItem(item); } } item.Properties["Monogram"] = monogram; return(item); }
public ActionResult AddToCart (ShirtVariation currentContent, decimal Quantity, string Monogram) // { // New var cart = _orderRepository.LoadOrCreateCart <ICart>( PrincipalInfo.CurrentPrincipal.GetContactId(), "Default"); //OrderContext.Current.GetCart() // new line, check in TrousersController cart.Properties["SpecialShip"] = false; string code = currentContent.Code; var lineItem = cart.GetAllLineItems().SingleOrDefault(x => x.Code == code); if (lineItem == null) { lineItem = _orderGroupFactory.CreateLineItem(code, cart); lineItem.Quantity = Quantity; _placedPriceProcessor.UpdatePlacedPrice (lineItem, GetContact(), _currentMarket.GetCurrentMarket().MarketId, cart.Currency , (lineItemToValidate, ValidationIssue) => { }); // older var justChecking = lineItem.GetDiscountedPrice(cart.Currency); // new, just a check var dd = _promotionEngine.Evaluate(currentContent.ContentLink); if (dd.Count() != 0) { var ddd = dd.First().SavedAmount; } else { var ddd = 0; } //lineItem.TrySetDiscountValue(() //ILineItemDiscountAmount lineItemDiscountAmount = null; //lineItemDiscountAmount.EntryAmount = 10; cart.AddLineItem(lineItem); // new - need the RewardDescriptions as well // don't get the discount for the first LI...? cart.ApplyDiscounts(); } else { lineItem.Quantity += Quantity; } ccService.CheckOnLineItem(currentContent, lineItem); // new var d = lineItem.GetEntryDiscount(); #region Could be more pro-active - checking Market & Inventory before adding the LI var validationIssues = new Dictionary <ILineItem, ValidationIssue>(); var validLineItem = _lineItemValidator.Validate(lineItem, _currentMarket.GetCurrentMarket().MarketId , (item, issue) => ValidationIssuesClassLevel.Add(item, issue)); // crash here if the previous lines fond issues... like "not in market" cart.ValidateOrRemoveLineItems((item, issue) => validationIssues.Add(item, issue)); #endregion if (validLineItem) { // not good, but...as the SDK says /* -- Line item calculator -- * The LineItemCalculator calculates a line item's extended price * and discounted price.Extended price. Includes order-level discount amount * (spread over all line items in the shipment) and any line item discount amount. * Discounted price. Only calculates the price with the "line item discount amount," * that is, the discount for a specific item.*/ // ...have to do something like this for now - out-commented for now //LineItem li = (LineItem)lineItem; //li.LineItemDiscountAmount = (decimal)Session["SavedMoney"]; // ...and it gets into CM // doesn't seem "to bite" //lineItem.Properties["LineItemDiscountAmount"] = (decimal)Session["SavedMoney"]; //InMemoryLineItem memLI = (InMemoryLineItem)lineItem; lineItem.Properties["Monogram"] = Monogram; lineItem.Properties["RequireSpecialShipping"] = currentContent.RequireSpecialShipping; // Below - need to set the cart.Properties["SpecialShip"] = false; ... it's null now // Bug in 11, rewritten for now //ccService.IsSecondShipmentReqired(cart); ccService.CheckOnLineItems(cart); // new October 2017 - gives the discount set properly in the cart IEnumerable <RewardDescription> discounts = cart.ApplyDiscounts(); _orderRepository.Save(cart); // dummy for now... RoCe: Fix Session["SecondPayment"] = false; } string passingValue = cart.Name; // if needed, send something along ContentReference cartRef = _contentLoader.Get <StartPage>(ContentReference.StartPage).Settings.cartPage; // get to the cart page, if needed return(RedirectToAction("Index", new { node = cartRef, routedData = passingValue })); #region Checking on Redirects/Transfers // workaround for RedirectToAction (as we do from Cart --> CheckOut)... doesn´t work at the moment from ECF-pages //Url url = _urlResolver.GetUrl(cartRef); //CartPage cartPage = _contentLoader.Get<CartPage>(cartRef); //var lang2 = ContentLanguage.PreferredCulture; //var name = cartPage.Name; //return RedirectToAction("Index", lang2 + "/" + name, new { passedAlong = passingValue }); // Change this in "Fund" //return RedirectToAction("Index", "en/Cart", new { passedAlong = passingValue }); // works //string passingValue = ch.Cart.Name; // if needed, the cart for example //return RedirectToAction("Index", new { node = theRef, passedAlong = passingValue }); // nothing is happening //return (ActionResult)View(model); //nope //return RedirectToAction("Index", new { node = theRef, passedAlong = passingValue }); // njet //Server.Transfer("http://localhost:49918" + _urlResolver.GetUrl(theRef.ToPageReference())); // njet //return RedirectToAction("Index", new { node = theRef.ToPageReference(), passedAlong = passingValue }); //return RedirectToRoute() // return RedirectToAction( new RouteValueDictionary() // new{ // "controller" = Blah, // "action" = Blah, // "foo" = “Bar” // } //)); // var x = HttpContext.Server.TransferRequest(""); // Response.Redirect(_urlResolver.GetUrl(theRef)); // OK //return RedirectToAction("Index",url.Uri.OriginalString , new { passedAlong = passingValue }); // nope //Redirect(url.ToString()); //return null; #endregion }
public ActionResult AddToCart(ShirtVariation currentContent, decimal Quantity, string Monogram) { // LoadOrCreateCart - in EPiServer.Commerce.Order.IOrderRepositoryExtensions var cart = _orderRepository.LoadOrCreateCart <ICart>( PrincipalInfo.CurrentPrincipal.GetContactId(), "Default"); //cart. // ...have a look at all the extension methods string code = currentContent.Code; // Manually checking - could have a look at the InventoryRecord - properties // ...could be part of an optional lab in Fund IWarehouse wh = _whRep.GetDefaultWarehouse(); InventoryRecord rec = _invService.Get(code, wh.Code); // ... can get: Sequence contains more than one matching element... // ...when we have two different lineitems for the same SKU // Use when the cart is empty/one LI for SKU with Qty --> no crash var lineItem = cart.GetAllLineItems().SingleOrDefault(x => x.Code == code); //currentContent.IsAvailableInCurrentMarket(); // Below works for the same SKU on different LineItems... Changing back for the Fund //var lineItem = cart.GetAllLineItems().First(x => x.Code == code); // Changed to this for multiple LI with the same code - crash if no cart // ECF 12 changes - market.MarketId IMarket market = _currentMarket.GetCurrentMarket(); if (lineItem == null) { lineItem = _orderFactory.CreateLineItem(code, cart); lineItem.Quantity = Quantity; // gets this as an argument for the method // ECF 12 changes - market.MarketId _placedPriceProcessor.UpdatePlacedPrice (lineItem, GetContact(), market.MarketId, cart.Currency, (lineItemToValidate, validation) => { }); // does not take care of the messages here cart.AddLineItem(lineItem); } else { // Qty increases ... no new LineItem ... // original for Fund. lineItem.Quantity += Quantity; // need an update // maybe do price validation here too } // Validations var validationIssues = new Dictionary <ILineItem, ValidationIssue>(); // ECF 12 changes - market.MarketId // RoCe - This needs to be updated to get the message out + Lab-steps added... var validLineItem = _lineItemValidator.Validate(lineItem, market.MarketId, (item, issue) => { }); cart.ValidateOrRemoveLineItems((item, issue) => validationIssues.Add(item, issue), _lineItemValidator); //var someIssue = validationIssues.First().Value; if (validLineItem) // We're happy { // If MDP - error when adding, may need to reset IIS as the model has changed // when adding/removing the MetaField in CM lineItem.Properties["Monogram"] = Monogram; _orderRepository.Save(cart); } ContentReference cartRef = _contentLoader.Get <StartPage>(ContentReference.StartPage).Settings.CartPage; ContentReference cartPageRef = EPiServer.Web.SiteDefinition.Current.StartPage; CartPage cartPage = _contentLoader.Get <CartPage>(cartRef); var name = cartPage.Name; var lang = ContentLanguage.PreferredCulture; string passingValue = cart.Name; // if something is needed //return RedirectToAction("Index", new { node = theRef, passedAlong = passingValue }); // Doesn't work return(RedirectToAction("Index", lang + "/" + name, new { passedAlong = passingValue })); // Works }