public virtual void AddItem(Shop shop, Product product, IEnumerable<Variation> variations) { ShoppingBasket shoppingBasket = GetCurrentShoppingBasketInternal(shop, true); // If card is already in basket, just increment quantity, otherwise create a new item. ShoppingBasketItem item = shoppingBasket.GetChildren<ShoppingBasketItem>().SingleOrDefault(i => i.Product == product && ((variations == null && i.Variations == null) || EnumerableUtility.Equals(i.Variations, variations))); if (item == null) { VariationPermutation variationPermutation = null; if (variations != null && variations.Any()) { variationPermutation = new VariationPermutation(); foreach (Variation variation in variations) variationPermutation.Variations.Add(variation); } item = new ShoppingBasketItem { Product = product, VariationPermutation = variationPermutation, Quantity = 1 }; item.AddTo(shoppingBasket); } else { item.Quantity += 1; } _persister.Save(shoppingBasket); }
public static string ShoppingBasketSummary(this HtmlHelper html, Shop shop) { if (shop == null) return "[[Shop page not found]]"; ShoppingBasketPage shoppingBasketPage = shop.GetChild("shopping-basket") as ShoppingBasketPage; if (shoppingBasketPage == null) return "[[Shopping basket page not found]]"; IShoppingBasketService shoppingBasketService = Context.Current.Resolve<IShoppingBasketService>(); IShoppingBasket shoppingBasket = shoppingBasketService.GetBasket(shop); string innerText = string.Format("You have <span>{0}</span> items in your shopping basket (<span>{1:C2}</span>)", shoppingBasket.TotalItemCount, shoppingBasket.SubTotalPrice); TagBuilder linkTag = new TagBuilder("a"); linkTag.MergeAttribute("href", shoppingBasketPage.Url, true); linkTag.MergeAttribute("id", "basket", true); linkTag.SetInnerText(innerText); return linkTag.ToString(); }
public Order PlaceOrder(Shop shop, string cardNumber, string cardVerificationCode, ShoppingBasket shoppingBasket) { List<OrderItem> items = new List<OrderItem>(); foreach (IShoppingBasketItem shoppingBasketItem in items) { ProductOrderItem orderItem = new ProductOrderItem { WeakProductLink = shoppingBasketItem.Product.ID, ProductTitle = shoppingBasketItem.Product.Title, Quantity = shoppingBasketItem.Quantity, Price = shoppingBasketItem.Product.CurrentPrice, VATable = !(shoppingBasketItem.Product.VatZeroRated) }; if (shoppingBasketItem.Variations != null) foreach (Variation variation in shoppingBasketItem.Variations) orderItem.Variations.Add(variation.VariationSet.Title + ": " + variation.Title); items.Add(orderItem); } Order order = PlaceOrder(shop, cardNumber, cardVerificationCode, shoppingBasket.DeliveryMethod, shoppingBasket.DeliveryMethod.Price, (Address) shoppingBasket.BillingAddress.Clone(true), (Address) (shoppingBasket.ShippingAddress ?? shoppingBasket.BillingAddress).Clone(true), (PaymentCard) shoppingBasket.PaymentCard.Clone(true), shoppingBasket.EmailAddress, shoppingBasket.TelephoneNumber, shoppingBasket.MobileTelephoneNumber, items, shoppingBasket.TotalDeliveryPrice, shoppingBasket.TotalPrice); // Clear shopping basket. _persister.Delete(shoppingBasket); return order; }
public virtual ShoppingBasket GetBasket(Shop shop) { return GetCurrentShoppingBasketInternal(shop, false); }
public virtual void ClearBasket(Shop shop) { ShoppingBasket shoppingBasket = GetCurrentShoppingBasketInternal(shop, false); if (shoppingBasket != null && shoppingBasket.ID > 0) { _persister.Delete(shoppingBasket); _webContext.Response.Cookies.Remove(GetCookieKey(shop)); } }
private ShoppingBasket GetShoppingBasketFromCookie(Shop shop) { HttpCookie cookie = _webContext.Request.Cookies[GetCookieKey(shop)]; if (cookie == null) return null; string shopperID = cookie.Value; return _finder.QueryItems<ShoppingBasket>().SingleOrDefault(sb => sb.Name == shopperID); }
private ShoppingBasket GetCurrentShoppingBasketInternal(Shop shop, bool createBasket) { ShoppingBasket shoppingBasket = GetShoppingBasketFromCookie(shop); if (shoppingBasket != null) { // Check that products in shopping cart still exist, and if not remove those shopping cart items. List<ShoppingBasketItem> itemsToRemove = new List<ShoppingBasketItem>(); foreach (ShoppingBasketItem item in shoppingBasket.GetChildren<ShoppingBasketItem>()) if (item.Product == null) itemsToRemove.Add(item); foreach (ShoppingBasketItem item in itemsToRemove) shoppingBasket.Children.Remove(item); _persister.Save(shoppingBasket); } else { shoppingBasket = new ShoppingBasket { Name = Guid.NewGuid().ToString() }; if (shop.DeliveryMethods != null) shoppingBasket.DeliveryMethod = shop.DeliveryMethods.GetChildren<DeliveryMethod>().FirstOrDefault(); // don't always save basket to database // this will only happen when we add items to it if (createBasket) { shoppingBasket.AddTo(shop.ShoppingBaskets); _persister.Save(shoppingBasket); HttpCookie cookie = new HttpCookie(GetCookieKey(shop), shoppingBasket.Name); if (shop.PersistentShoppingBaskets) cookie.Expires = DateTime.Now.AddYears(1); _webContext.Response.Cookies.Add(cookie); } } return shoppingBasket; }
private static string GetCookieKey(Shop shop) { return "ZeusECommerce" + shop.ID; }
public virtual void UpdateQuantity(Shop shop, Product product, VariationPermutation variationPermutation, int newQuantity) { if (newQuantity < 0) throw new ArgumentOutOfRangeException("newQuantity", "Quantity must be greater than or equal to 0."); ShoppingBasket shoppingBasket = GetCurrentShoppingBasketInternal(shop, true); ShoppingBasketItem item = shoppingBasket.GetChildren<ShoppingBasketItem>().SingleOrDefault(i => i.Product == product && i.VariationPermutation == variationPermutation); if (item == null) return; if (newQuantity == 0) { shoppingBasket.Children.Remove(item); _persister.Delete(item); } else item.Quantity = newQuantity; _persister.Save(shoppingBasket); }
public virtual void SaveBasket(Shop shop) { _persister.Save(GetCurrentShoppingBasketInternal(shop, true)); }
public virtual void RemoveItem(Shop shop, Product product, VariationPermutation variationPermutation) { UpdateQuantity(shop, product, variationPermutation, 0); }