コード例 #1
0
        public List <Dictionary <string, object> > GenerateProductStockFeed()
        {
            var productFeed = new List <Dictionary <string, object> >();

            var products = _productService.SearchProducts(visibleIndividuallyOnly: true);

            foreach (var product in products)
            {
                var productsToProcess = new List <Product>();
                switch (product.ProductType)
                {
                case ProductType.SimpleProduct:
                {
                    //simple product doesn't have child products
                    productsToProcess.Add(product);
                }
                break;

                case ProductType.GroupedProduct:
                {
                    //grouped products could have several child products
                    var associatedProducts = _productService.GetAssociatedProducts(product.Id);
                    productsToProcess.AddRange(associatedProducts);
                }
                break;

                default:
                    continue;
                }

                foreach (var productToProcess in productsToProcess)
                {
                    var productInfo = new Dictionary <string, object>
                    {
                        { "id", productToProcess.Id.ToString() },
                        {
                            "product_availability",
                            product.AvailableEndDateTimeUtc != null
                                ? product.AvailableEndDateTimeUtc.Value.ToString("yy-MM-dd hh:mm:ss")
                                : null
                        }
                    };

                    decimal price;
                    decimal priceWithDiscount;
                    GetProductPrice(product, out price, out priceWithDiscount);

                    productInfo.Add("price", price.ToString(new CultureInfo("en-US", false).NumberFormat));
                    productInfo.Add("promo", priceWithDiscount.ToString(new CultureInfo("en-US", false).NumberFormat));
                    productInfo.Add("promo_price_end_date", null);

                    var inventory  = new Dictionary <string, object>();
                    var attributes = new Dictionary <string, object>();

                    var allAttributesXml = _productAttributeParser.GenerateAllCombinations(product, true);
                    foreach (var attributesXml in allAttributesXml)
                    {
                        var warnings = new List <string>();
                        warnings.AddRange(_shoppingCartService.GetShoppingCartItemAttributeWarnings(_workContext.CurrentCustomer,
                                                                                                    ShoppingCartType.ShoppingCart, product, 1, attributesXml, true));
                        if (warnings.Count != 0)
                        {
                            continue;
                        }

                        var inStock             = true;
                        var existingCombination = _productAttributeParser.FindProductAttributeCombination(product, attributesXml);
                        if (existingCombination != null)
                        {
                            inStock = existingCombination.StockQuantity > 0;
                        }

                        var varCode = GetCombinationCode(attributesXml);
                        if (!attributes.ContainsKey(varCode))
                        {
                            attributes.Add(varCode, inStock);
                        }
                    }

                    var variation = new Dictionary <string, object>
                    {
                        { "variation", attributes }
                    };

                    if (attributes.Count > 0)
                    {
                        inventory.Add("variations", true);
                        inventory.Add("stock", variation);
                    }
                    else
                    {
                        inventory.Add("variations", false);
                        inventory.Add("stock", product.StockQuantity > 0);
                    }

                    productInfo.Add("inventory", inventory);
                    productInfo.Add("user_groups", false);

                    productFeed.Add(productInfo);
                }
            }

            return(productFeed);
        }