public JsonResult GetCurrentProductStockInfo(ProductStockInfoInputModel model)
        {
            try
            {
                Assert.ArgumentNotNull(model, "model");

                var validationResult = new BaseJsonResult();
                this.ValidateModel(validationResult);
                if (validationResult.HasErrors)
                {
                    return(Json(validationResult, JsonRequestBehavior.AllowGet));
                }

                var currentProductItem = SearchNavigation.GetProduct(model.ProductId, CurrentCatalog.Name);
                var productId          = currentProductItem.Name;
                var catalogName        = currentProductItem["CatalogName"];
                var products           = new List <CommerceInventoryProduct>();
                if (currentProductItem.HasChildren)
                {
                    foreach (Item item in currentProductItem.Children)
                    {
                        products.Add(new CommerceInventoryProduct
                        {
                            ProductId   = productId,
                            CatalogName = catalogName,
                            VariantId   = item["VariantId"]
                        });
                    }
                }
                else
                {
                    products.Add(new CommerceInventoryProduct {
                        ProductId = productId, CatalogName = catalogName
                    });
                }

                var response = this.InventoryManager.GetStockInformation(this.CurrentStorefront, products, StockDetailsLevel.All);
                var result   = new StockInfoListBaseJsonResult(response.ServiceProviderResult);
                if (response.Result == null)
                {
                    return(Json(result, JsonRequestBehavior.AllowGet));
                }

                result.Initialize(response.Result);
                var stockInfo = response.Result.FirstOrDefault();
                if (stockInfo != null)
                {
                    this.InventoryManager.VisitedProductStockStatus(this.CurrentStorefront, stockInfo, string.Empty);
                }

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                CommerceLog.Current.Error("GetCurrentProductStockInfo", this, e);
                return(Json(new BaseJsonResult("GetCurrentProductStockInfo", e), JsonRequestBehavior.AllowGet));
            }
        }
        public JsonResult GetCurrentProductStockInfo(ProductStockInfoInputModel model)
        {
            try
            {
                if (CatalogManager.CatalogContext == null || StorefrontContext.Current == null)
                {
                    throw new InvalidOperationException("Cannot be called without a valid catalog context.");
                }

                Assert.ArgumentNotNull(model, nameof(model));

                var validationResult = this.CreateJsonResult();
                if (validationResult.HasErrors)
                {
                    return(Json(validationResult, JsonRequestBehavior.AllowGet));
                }

                var productId = model.ProductId;
                var response  = CatalogManager.GetProductInventory(productId);
                var result    = new StockInfoListApiModel(response.ServiceProviderResult);
                if (response.Result == null)
                {
                    return(Json(result, JsonRequestBehavior.AllowGet));
                }

                result.Initialize(response.Result);
                var stockInfo = response.Result.FirstOrDefault();
                if (stockInfo != null)
                {
                    InventoryManager.VisitedProductStockStatus(stockInfo, string.Empty);
                }

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(new ErrorApiModel("GetCurrentProductStockInfo", e), JsonRequestBehavior.AllowGet));
            }
        }
Example #3
0
        public JsonResult GetCurrentProductStockInfo(ProductStockInfoInputModel model)
        {
            try
            {
                Assert.ArgumentNotNull(model, "model");

                var validationResult = new BaseJsonResult();
                this.ValidateModel(validationResult);

                if (validationResult.HasErrors)
                {
                    return(Json(validationResult, JsonRequestBehavior.AllowGet));
                }

                var result = _catalogRepository.GetCurrentProductStockInfo(model.ProductId);

                return(Json(result, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                _logger.LogError("GetCurrentProductStockInfo", this, e);
                return(Json(new BaseJsonResult("GetCurrentProductStockInfo", e), JsonRequestBehavior.AllowGet));
            }
        }