コード例 #1
0
        public void Can_parse_required_productvariant_ids()
        {
            var productVariant = new ProductVariant
            {
                RequiredProductVariantIds = "1, 4,7 ,a,"
            };

            var ids = productVariant.ParseRequiredProductVariantIds();
            ids.Length.ShouldEqual(3);
            ids[0].ShouldEqual(1);
            ids[1].ShouldEqual(4);
            ids[2].ShouldEqual(7);
        }
コード例 #2
0
        /// <summary>
        /// Validates required product variants (product variants which require other variant to be added to the cart)
        /// </summary>
        /// <param name="customer">Customer</param>
        /// <param name="shoppingCartType">Shopping cart type</param>
        /// <param name="productVariant">Product variant</param>
        /// <param name="storeId">Store identifier</param>
        /// <param name="automaticallyAddRequiredProductVariantsIfEnabled">Automatically add required product variants if enabled</param>
        /// <returns>Warnings</returns>
        public virtual IList<string> GetRequiredProductVariantWarnings(Customer customer, 
            ShoppingCartType shoppingCartType, ProductVariant productVariant, 
            int storeId, bool automaticallyAddRequiredProductVariantsIfEnabled)
        {
            if (customer == null)
                throw new ArgumentNullException("customer");

            if (productVariant == null)
                throw new ArgumentNullException("productVariant");

            var cart = customer.ShoppingCartItems
                .Where(sci => sci.ShoppingCartType == shoppingCartType)
                .Where(sci => sci.StoreId == storeId)
                .ToList();

            var warnings = new List<string>();

            if (productVariant.RequireOtherProducts)
            {
                var requiredProductVariants = new List<ProductVariant>();
                foreach (var id in productVariant.ParseRequiredProductVariantIds())
                {
                    var rpv = _productService.GetProductVariantById(id);
                    if (rpv != null)
                        requiredProductVariants.Add(rpv);
                }

                foreach (var rpv in requiredProductVariants)
                {
                    //ensure that product is in the cart
                    bool alreadyInTheCart = false;
                    foreach (var sci in cart)
                    {
                        if (sci.ProductVariantId == rpv.Id)
                        {
                            alreadyInTheCart = true;
                            break;
                        }
                    }
                    //not in the cart
                    if (!alreadyInTheCart)
                    {

                        string fullProductName = "";
                        if (!String.IsNullOrEmpty(rpv.GetLocalized(x => x.Name)))
                            fullProductName = string.Format("{0} ({1})", rpv.Product.GetLocalized(x => x.Name), rpv.GetLocalized(x => x.Name));
                        else
                            fullProductName = rpv.Product.GetLocalized(x => x.Name);

                        if (productVariant.AutomaticallyAddRequiredProductVariants)
                        {
                            //add to cart (if possible)
                            if (automaticallyAddRequiredProductVariantsIfEnabled)
                            {
                                //pass 'false' for 'automaticallyAddRequiredProductVariantsIfEnabled' to prevent circular references
                                var addToCartWarnings = AddToCart(customer, rpv, shoppingCartType, storeId, "", decimal.Zero, 1, 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"), fullProductName));
                                }
                            }
                            else
                            {
                                warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.RequiredProductWarning"), fullProductName));
                            }
                        }
                        else
                        {
                            warnings.Add(string.Format(_localizationService.GetResource("ShoppingCart.RequiredProductWarning"), fullProductName));
                        }
                    }
                }
            }

            return warnings;
        }