Exemple #1
0
        /// <summary>
        /// Initializes the specified stock infos.
        /// </summary>
        /// <param name="stockInfo">The stock information.</param>
        public virtual void Initialize(StockInformation stockInfo)
        {
            Assert.ArgumentNotNull(stockInfo, "stockInfo");

            if (stockInfo == null || stockInfo.Status == null)
            {
                return;
            }

            this.ProductId = stockInfo.Product.ProductId;
            this.VariantId = string.IsNullOrEmpty(((CommerceInventoryProduct)stockInfo.Product).VariantId) ? string.Empty : ((CommerceInventoryProduct)stockInfo.Product).VariantId;
            this.Status    = StorefrontManager.GetProductStockStatusName(stockInfo.Status);
            this.Count     = stockInfo.Count < 0 ? 0 : stockInfo.Count;
            if (stockInfo.AvailabilityDate != null & stockInfo.AvailabilityDate.HasValue)
            {
                this.AvailabilityDate = stockInfo.AvailabilityDate.Value.ToDisplayedDate();
            }
        }
        /// <summary>
        /// Populates the stock information
        /// </summary>
        /// <param name="model">The product model</param>
        protected void PopulateStockInformation(ProductViewModel model)
        {
            //Check for Gift Card
            if (model.ProductId == StorefrontManager.CurrentStorefront.GiftCardProductId)
            {
                //Gift cards are always in stock..
                model.StockStatus     = StockStatus.InStock;
                model.StockStatusName = StorefrontManager.GetProductStockStatusName(StockStatus.InStock);
                return;
            }

            var inventoryProducts = new List <CommerceInventoryProduct> {
                new CommerceInventoryProduct {
                    ProductId = model.ProductId, CatalogName = model.CatalogName
                }
            };
            var response = this.InventoryManager.GetStockInformation(this.CurrentStorefront, inventoryProducts, StockDetailsLevel.StatusAndAvailability);

            if (!response.ServiceProviderResult.Success || response.Result == null)
            {
                return;
            }

            var stockInfos = response.Result;
            var stockInfo  = stockInfos.FirstOrDefault();

            if (stockInfo == null || stockInfo.Status == null)
            {
                return;
            }

            model.StockStatus     = stockInfo.Status;
            model.StockStatusName = StorefrontManager.GetProductStockStatusName(model.StockStatus);
            if (stockInfo.AvailabilityDate != null)
            {
                model.StockAvailabilityDate = stockInfo.AvailabilityDate.Value.ToDisplayedDate();
            }
        }
Exemple #3
0
        /// <summary>
        /// Gets the index of the product stock status from.
        /// </summary>
        /// <param name="viewModelList">The view model list.</param>
        public static void GetProductStockStatusFromIndex(List <ProductViewModel> viewModelList)
        {
            if (viewModelList == null || viewModelList.Count == 0)
            {
                return;
            }

            var searchManager = CommerceTypeLoader.CreateInstance <ICommerceSearchManager>();
            var searchIndex   = searchManager.GetIndex();

            using (var context = searchIndex.CreateSearchContext())
            {
                var predicate = PredicateBuilder.Create <InventorySearchResultItem>(item => item[StorefrontConstants.KnownIndexFields.InStockLocations].Contains("Default"));
                predicate = predicate.Or(item => item[StorefrontConstants.KnownIndexFields.OutOfStockLocations].Contains("Default"));
                predicate = predicate.Or(item => item[StorefrontConstants.KnownIndexFields.OrderableLocations].Contains("Default"));
                predicate = predicate.Or(item => item[StorefrontConstants.KnownIndexFields.PreOrderable].Contains("0"));

                var searchResults = context.GetQueryable <InventorySearchResultItem>()
                                    .Where(item => item.CommerceSearchItemType == CommerceSearchResultItemType.Product)
                                    .Where(item => item.Language == SearchNavigation.CurrentLanguageName)
                                    .Where(BuildProductIdListPredicate(viewModelList))
                                    .Where(predicate)
                                    .Select(x => new { x.OutOfStockLocations, x.OrderableLocations, x.PreOrderable, x.InStockLocations, x.Fields, x.Name });

                var results = searchResults.GetResults();
                if (results.TotalSearchResults == 0)
                {
                    return;
                }

                foreach (var result in results)
                {
                    var resultDocument = result.Document;
                    if (resultDocument == null)
                    {
                        continue;
                    }

                    StockStatus status = null;

                    var isInStock = resultDocument.Fields.ContainsKey(StorefrontConstants.KnownIndexFields.InStockLocations) &&
                                    resultDocument.Fields[StorefrontConstants.KnownIndexFields.InStockLocations] != null;
                    if (isInStock)
                    {
                        status = StockStatus.InStock;
                    }
                    else
                    {
                        var isPreOrderable = resultDocument.Fields.ContainsKey(StorefrontConstants.KnownIndexFields.PreOrderable) &&
                                             result.Document.PreOrderable != null &&
                                             (result.Document.PreOrderable.Equals("1", StringComparison.OrdinalIgnoreCase) ||
                                              result.Document.PreOrderable.Equals("true", StringComparison.OrdinalIgnoreCase));
                        if (isPreOrderable)
                        {
                            status = StockStatus.PreOrderable;
                        }
                        else
                        {
                            var isOutOfStock = resultDocument.Fields.ContainsKey(StorefrontConstants.KnownIndexFields.OutOfStockLocations) &&
                                               result.Document.OutOfStockLocations != null;
                            var isBackOrderable = resultDocument.Fields.ContainsKey(StorefrontConstants.KnownIndexFields.OrderableLocations) &&
                                                  result.Document.OrderableLocations != null;
                            if (isOutOfStock && isBackOrderable)
                            {
                                status = StockStatus.BackOrderable;
                            }
                            else
                            {
                                status = isOutOfStock ? StockStatus.OutOfStock : null;
                            }
                        }
                    }

                    var foundModel = viewModelList.Find(x => x.ProductId == result.Document.Name);
                    if (foundModel != null)
                    {
                        foundModel.StockStatus     = status;
                        foundModel.StockStatusName = StorefrontManager.GetProductStockStatusName(foundModel.StockStatus);
                    }
                }
            }
        }