public void Can_parse_required_productvariant_ids() { var product = new Product { RequiredProductIds = "1, 4,7 ,a," }; var ids = product.ParseRequiredProductIds(); ids.Length.ShouldEqual(3); ids[0].ShouldEqual(1); ids[1].ShouldEqual(4); ids[2].ShouldEqual(7); }
/// <summary> /// Validates required products (products which require some other products to be added to the cart) /// </summary> /// <param name="customer">Customer</param> /// <param name="shoppingCartType">Shopping cart type</param> /// <param name="product">Product</param> /// <param name="storeId">Store identifier</param> /// <param name="automaticallyAddRequiredProductsIfEnabled">Automatically add required products if enabled</param> /// <returns>Warnings</returns> public virtual IList<string> GetRequiredProductWarnings(Customer customer, ShoppingCartType shoppingCartType, Product product, int storeId, bool automaticallyAddRequiredProductsIfEnabled) { if (customer == null) throw new ArgumentNullException("customer"); if (product == null) throw new ArgumentNullException("product"); var cart = customer.ShoppingCartItems .Where(sci => sci.ShoppingCartType == shoppingCartType) .LimitPerStore(storeId) .ToList(); var warnings = new List<string>(); if (product.RequireOtherProducts) { var requiredProducts = new List<Product>(); foreach (var id in product.ParseRequiredProductIds()) { var rp = _productService.GetProductById(id); if (rp != null) requiredProducts.Add(rp); } foreach (var rp in requiredProducts) { //ensure that product is in the cart bool alreadyInTheCart = false; foreach (var sci in cart) { if (sci.ProductId == rp.ID) { alreadyInTheCart = true; break; } } //not in the cart if (!alreadyInTheCart) { if (product.AutomaticallyAddRequiredProducts) { //add to cart (if possible) if (automaticallyAddRequiredProductsIfEnabled) { //pass 'false' for 'automaticallyAddRequiredProductsIfEnabled' to prevent circular references var addToCartWarnings = AddToCart(customer: customer, product: rp, shoppingCartType: shoppingCartType, storeId: storeId, automaticallyAddRequiredProductsIfEnabled: false); if (addToCartWarnings.Count > 0) { //a product wasn't atomatically added for some reasons //don't display specific errors from 'addToCartWarnings' variable //display only generic error warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.RequiredProductWarning"), rp.GetLocalized(x => x.Name))); } } else { warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.RequiredProductWarning"), rp.GetLocalized(x => x.Name))); } } else { warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.RequiredProductWarning"), rp.GetLocalized(x => x.Name))); } } } } return warnings; }