/// <summary> /// Handles the PostAcquireRequestState event of the context control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> void context_PostAcquireRequestState(object sender, EventArgs e) { if (IsResourceFile() || IsWebApi) return; var application = (HttpApplication)sender; var context = application.Context; var session = CustomerSession; Populate(context, session); //Price list can only be evaluated after all customer tagsets are ready var priceListHelper = DependencyResolver.Current.GetService<PriceListClient>(); var priceLists = priceListHelper.GetPriceListStack(session.CatalogId, session.Currency, session.GetCustomerTagSet()); session.Pricelists = priceLists; //Update prices in current currency var helper = new CartHelper(CartHelper.CartName); if (!helper.IsEmpty && !string.IsNullOrEmpty(session.Currency) && !string.IsNullOrEmpty(helper.Cart.BillingCurrency) && session.Currency != helper.Cart.BillingCurrency) { helper.RunWorkflow("ShoppingCartValidateWorkflow"); helper.SaveChanges(); } }
public IHttpActionResult EstimatePost(ShippingEstimateModel shippingEstimateModel) { if (ModelState.IsValid) { var helper = new CartHelper(CartHelper.CartName); //Find best shipping method var store = StoreHelper.StoreClient.GetCurrentStore(); var storeShippingMethods = helper.ShippingClient.GetAllShippingMethods() .Where(sm => sm.PaymentMethodShippingMethods.Select(x => x.PaymentMethod) .Any(pm => store.PaymentGateways.Any(pg => pg.PaymentGateway == pm.Name))) .Select(s=>s.ShippingMethodId).ToList(); var bestShipping = helper.GetShippingMethods(storeShippingMethods).Min(); //Update line items foreach (var lineItem in helper.OrderForm.LineItems) { lineItem.ShippingMethodId = bestShipping.Id; lineItem.ShippingMethodName = bestShipping.DisplayName; } // run workflow helper.RunWorkflow("ShoppingCartPrepareWorkflow"); helper.SaveChanges(); return Ok(bestShipping); } return BadRequest(); }
public HttpResponseMessage EstimatePost(ShippingEstimateModel shippingEstimateModel) { if (ModelState.IsValid) { var helper = new CartHelper(CartHelper.CartName); //Find best shipping method var store = StoreHelper.StoreClient.GetCurrentStore(); var storeShippingMethods = helper.ShippingClient.GetAllShippingMethods() .Where(sm => sm.PaymentMethodShippingMethods.Select(x => x.PaymentMethod) .Any(pm => store.PaymentGateways.Any(pg => pg.PaymentGateway == pm.Name))) .Select(s=>s.ShippingMethodId).ToList(); var bestShipping = helper.GetShippingMethods(storeShippingMethods).Min(); //Update line items foreach (var lineItem in helper.OrderForm.LineItems) { lineItem.ShippingMethodId = bestShipping.Id; lineItem.ShippingMethodName = bestShipping.DisplayName; } // run workflow helper.RunWorkflow("ShoppingCartPrepareWorkflow"); helper.SaveChanges(); var response = Request.CreateResponse(HttpStatusCode.OK); var bShippingJson = System.Web.Helpers.Json.Encode(bestShipping); response.Content = new StringContent(bShippingJson); return response; } return Request.CreateResponse(HttpStatusCode.BadRequest); }
/// <summary> /// Profile migrate from anonymous. /// </summary> /// <param name="sender">The sender.</param> /// <param name="pe">The <see cref="ProfileMigrateEventArgs"/> instance containing the event data.</param> private void Profile_MigrateAnonymous(Object sender, ProfileMigrateEventArgs pe) { var client = ServiceLocator.Current.GetInstance<OrderClient>(); var orders = client.GetAllCustomerOrders(pe.AnonymousID, UserHelper.CustomerSession.StoreId); if (orders != null) { foreach (var order in orders) { order.CustomerId = UserHelper.CustomerSession.CustomerId; order.CustomerName = UserHelper.CustomerSession.CustomerName; } client.SaveChanges(); } // Migrate shopping cart var cart = new CartHelper(CartHelper.CartName); var anonymousCart = new CartHelper(CartHelper.CartName, pe.AnonymousID); // Only perform merge if cart is not empty if (!anonymousCart.IsEmpty) { // Merge cart cart.Add(anonymousCart.Cart, true); cart.SaveChanges(); // Delete anonymous cart anonymousCart.Delete(); anonymousCart.SaveChanges(); } var wishList = new CartHelper(CartHelper.WishListName); var anonymousWishList = new CartHelper(CartHelper.WishListName, pe.AnonymousID); // Only perform merge if cart is not empty if (!anonymousWishList.IsEmpty) { // Merge wish list wishList.Add(anonymousWishList.Cart, true); if (String.IsNullOrEmpty(wishList.Cart.BillingCurrency)) { wishList.Cart.BillingCurrency = UserHelper.CustomerSession.Currency; } wishList.SaveChanges(); // Delete anonymous wish list anonymousWishList.Delete(); anonymousWishList.SaveChanges(); } //Delete the anonymous data from the database //ProfileManager.DeleteProfile(pe.AnonymousID); //Remove the anonymous identifier from the request so //this event will no longer fire for a logged-in user AnonymousIdentificationModule.ClearAnonymousIdentifier(); }
public ActionResult UpdateWishList(List<LineItemUpdateModel> lineItems, string action) { var ch = new CartHelper(CartHelper.CartName); var helper = new CartHelper(CartHelper.WishListName); if (action == UserHelper.AddToCartAction) { //add all to cart foreach (var lineItem in lineItems) { var li = helper.LineItems.FirstOrDefault(item => item.LineItemId == lineItem.LineItemId); if (li == null) { continue; } var catalogItem = _catalogClient.GetItem(li.CatalogItemId); var parentItem = _catalogClient.GetItem(li.ParentCatalogItemId); ch.AddItem(catalogItem, parentItem, lineItem.Quantity, false); helper.Remove(li); // If wishlist is empty, remove it from the database if (helper.IsEmpty) { helper.Delete(); } } ch.SaveChanges(); } else { foreach (var lineItem in lineItems) { var li = helper.LineItems.FirstOrDefault(item => item.LineItemId == lineItem.LineItemId); if (li == null) { continue; } li.Comment = lineItem.Comment; li.Quantity = lineItem.Quantity; } } helper.SaveChanges(); return RedirectToAction("WishList"); }
/// <summary> /// Saves the cart changes. /// </summary> /// <param name="helper">The helper.</param> /// <returns>Dictionary of errors if any.</returns> private Dictionary<string, string> SaveChanges(CartHelper helper) { if (helper.Cart.Name != CartHelper.CartName) { _catalogClient.CustomerSession["SkipRewards"] = true; } var errors = new Dictionary<string, string>(); if (helper.Cart.Name == CartHelper.CartName) { var result = helper.RunWorkflow("ShoppingCartValidateWorkflow"); if (result.WorkflowResult.Warnings != null) { foreach (var warning in result.WorkflowResult.Warnings.Distinct()) { errors.Add(warning.Code, warning.Parameters["Message"]); } } } helper.SaveChanges(); return errors; }
/// <summary> /// Reorders the specified order. /// </summary> /// <param name="orderId">The order identifier.</param> /// <returns>ActionResult.</returns> /// <exception cref="System.UnauthorizedAccessException"></exception> public ActionResult Reorder(string orderId) { var order = _orderClient.GetCustomerOrder(UserHelper.CustomerSession.CustomerId, orderId); if (order == null || String.IsNullOrEmpty(order.CustomerId)) { throw new UnauthorizedAccessException(); } //Convert order forms to shopping cart order.OrderForms.ForEach(f=>f.Name = CartHelper.CartName); var ch = new CartHelper(CartHelper.CartName); ch.Add(order); if (String.IsNullOrEmpty(ch.Cart.BillingCurrency)) { ch.Cart.BillingCurrency = UserHelper.CustomerSession.Currency; } // run workflows ch.RunWorkflow("ShoppingCartPrepareWorkflow"); ch.SaveChanges(); return RedirectToAction("Index", "Checkout"); }
/// <summary> /// Saves the cart changes. /// </summary> /// <param name="helper">The helper.</param> /// <returns>Dictionary of errors if any.</returns> private Dictionary<string, string> SaveChanges(CartHelper helper) { if (helper.Cart.Name != CartHelper.CartName) { _catalogClient.CustomerSession["SkipRewards"] = true; } var errors = new Dictionary<string, string>(); if (helper.Cart.Name == CartHelper.CartName) { //Need to prepare cart because shipment rates could chaange after items added/removed //var result = helper.RunWorkflow("ShoppingCartPrepareWorkflow"); var result = helper.RunWorkflow("ShoppingCartValidateWorkflow"); if (result.WorkflowResult.Warnings != null) { foreach (var warning in result.WorkflowResult.Warnings.Distinct()) { errors.Add(warning.Code, warning.Parameters["Message"]); } } } helper.SaveChanges(); return errors; }
/// <summary> /// Saves the cart changes. /// </summary> /// <param name="helper">The helper.</param> /// <returns>Dictionary of errors if any.</returns> private Dictionary<string, string> SaveChanges(CartHelper helper) { if (helper.Cart.Name != CartHelper.CartName) { _catalogClient.CustomerSession["SkipRewards"] = true; } var errors = new Dictionary<string, string>(); if (helper.Cart.Name == CartHelper.CartName) { helper.RunWorkflow("ShoppingCartValidateWorkflow", errors); } helper.SaveChanges(); return errors; }