public virtual Variant ToLiquidVariant(storefrontModel.Product product)
        {
            var result = new Variant();

            result.Available             = true;
            result.Barcode               = product.Gtin;
            result.CatalogId             = product.CatalogId;
            result.CategoryId            = product.CategoryId;
            result.Id                    = product.Id;
            result.InventoryPolicy       = "continue";
            result.InventoryQuantity     = product.Inventory != null ? product.Inventory.InStockQuantity ?? 0 : 0;
            result.Options               = product.VariationProperties.Where(p => !string.IsNullOrEmpty(p.Value)).Select(p => p.Value).ToArray();
            result.CompareAtPrice        = product.Price.ListPrice.Amount * 100;
            result.CompareAtPriceWithTax = product.Price.ListPriceWithTax.Amount * 100;
            result.Price                 = product.Price.ActualPrice.Amount * 100;
            result.PriceWithTax          = product.Price.ActualPriceWithTax.Amount * 100;
            result.Selected              = false;
            result.Sku                   = product.Sku;
            result.Title                 = product.Name;
            result.Url                   = product.Url;
            result.Weight                = product.Weight ?? 0m;
            result.WeightUnit            = product.WeightUnit;
            result.FeaturedImage         = product.PrimaryImage != null?product.PrimaryImage.ToShopifyModel() : null;

            if (result.FeaturedImage != null)
            {
                result.FeaturedImage.ProductId         = product.Id;
                result.FeaturedImage.AttachedToVariant = true;
                result.FeaturedImage.Variants          = new[] { result };
            }

            return(result);
        }
        public async Task<ActionResult> GetActualProductPricesJson(Product[] products)
        {
            var prices = new List<ProductPrice>();

            if (products == null)
            {
                return Json(prices, JsonRequestBehavior.AllowGet);
            }

            var pricesResponse = await _pricingApi.PricingModuleEvaluatePricesAsync(
                evalContextProductIds: products.Select(p => p.Id).ToList(),
                evalContextCatalogId: WorkContext.CurrentStore.Catalog,
                evalContextCurrency: WorkContext.CurrentCurrency.Code,
                evalContextCustomerId: WorkContext.CurrentCustomer.Id,
                evalContextLanguage: WorkContext.CurrentLanguage.CultureName,
                evalContextStoreId: WorkContext.CurrentStore.Id);

            if (pricesResponse == null)
            {
                return Json(prices, JsonRequestBehavior.AllowGet);
            }

            prices = pricesResponse.Select(p => p.ToWebModel()).ToList();
            var promotionContext = WorkContext.ToPromotionEvaluationContext();
            promotionContext.PromoEntries = GetPromoEntries(products, prices);

            foreach (var product in products)
            {
                product.Currency = WorkContext.CurrentCurrency;
                product.Price = prices.FirstOrDefault(p => p.ProductId == product.Id);
            }
            await _promotionEvaluator.EvaluateDiscountsAsync(promotionContext, products);

            return Json(prices, JsonRequestBehavior.AllowGet);
        }
示例#3
0
        public async Task<CartBuilder> AddItemAsync(Product product, int quantity)
        {
            AddLineItem(product.ToLineItem(_language, quantity));

            await EvaluatePromotionsAsync();

            return this;
        }
        public static Product ToWebModel(this VirtoCommerceCatalogModuleWebModelProduct product, Language currentLanguage, Currency currentCurrency)
        {
            var retVal = new Product();

            retVal.Currency = currentCurrency;
            retVal.Price = new ProductPrice(currentCurrency);

            retVal.InjectFrom(product);

            retVal.Sku = product.Code;

            if(product.Category != null)
            {
                retVal.Category = product.Category.ToWebModel();
            }

            if (product.Properties != null)
            {
                retVal.Properties = product.Properties.Where(x => !String.Equals(x.Type, "Variation", StringComparison.InvariantCultureIgnoreCase))
                                                      .Select(p => p.ToWebModel(currentLanguage))
                                                      .ToList();
                retVal.VariationProperties = product.Properties.Where(x => String.Equals(x.Type, "Variation", StringComparison.InvariantCultureIgnoreCase))
                                                      .Select(p => p.ToWebModel(currentLanguage))
                                                      .ToList();
            }
            if (product.Images != null)
            {
                retVal.Images = product.Images.Select(i => i.ToWebModel()).ToArray();
                retVal.PrimaryImage = retVal.Images.FirstOrDefault(x => String.Equals(x.Url, product.ImgSrc, StringComparison.InvariantCultureIgnoreCase));
            }

            if (product.Assets != null)
            {
                retVal.Assets = product.Assets.Select(x => x.ToWebModel()).ToList();
            }

            if (product.Variations != null)
            {
                retVal.Variations = product.Variations.Select(v => v.ToWebModel(currentLanguage, currentCurrency)).ToList();
            }

            if (product.SeoInfos != null)
                retVal.SeoInfo = product.SeoInfos.Select(s => s.ToWebModel()).FirstOrDefault();

            if (product.Reviews != null)
            {
                retVal.Descriptions = product.Reviews.Select(r => new LocalizedString(new Language(r.LanguageCode), r.Content)).ToList();
                retVal.Description = retVal.Descriptions.Where(x => x.Language.Equals(currentLanguage))
                                                        .Select(x => x.Value)
                                                        .FirstOrDefault();
            }

            return retVal;
        }
        public async Task<ActionResult> GetActualProductPricesJson(Product[] products)
        {
            var prices = new List<ProductPrice>();

            if (products == null)
            {
                return Json(prices, JsonRequestBehavior.AllowGet);
            }

            var pricesResponse = await _pricingApi.PricingModuleEvaluatePricesAsync(
                evalContextProductIds: products.Select(p => p.Id).ToList(),
                evalContextCatalogId: WorkContext.CurrentStore.Catalog,
                evalContextCurrency: WorkContext.CurrentCurrency.Code,
                evalContextCustomerId: WorkContext.CurrentCustomer.Id,
                evalContextLanguage: WorkContext.CurrentLanguage.CultureName,
                evalContextStoreId: WorkContext.CurrentStore.Id);

            if (pricesResponse == null)
            {
                return Json(prices, JsonRequestBehavior.AllowGet);
            }

            prices = pricesResponse.Select(p => p.ToWebModel()).ToList();
            var promotionContext = new PromotionEvaluationContext
            {
                CartPromoEntries = GetCartPromoEntries(WorkContext.CurrentCart),
                Currency = WorkContext.CurrentCurrency,
                CustomerId = WorkContext.CurrentCustomer.Id,
                IsRegisteredUser = WorkContext.CurrentCustomer.HasAccount,
                Language = WorkContext.CurrentLanguage,
                PromoEntries = GetPromoEntries(products, prices),
                StoreId = WorkContext.CurrentStore.Id
            };

            foreach (var product in products)
            {
                product.Currency = WorkContext.CurrentCurrency;
                product.Price = prices.FirstOrDefault(p => p.ProductId == product.Id);
            }

            await _promotionEvaluator.EvaluateDiscountsAsync(promotionContext, products);
            prices = products.Select(p => p.Price).ToList();

            foreach (var price in prices)
            {
                if (price.ActiveDiscount != null)
                {
                    price.AbsoluteBenefit += price.ActiveDiscount.Amount;
                    price.ActualPrice = price.SalePrice - price.AbsoluteBenefit;
                }
            }

            return Json(prices, JsonRequestBehavior.AllowGet);
        }
 public async Task<ActionResult> GetActualProductPricesJson(Product[] products)
 {
     if(products == null)
     {
         throw new ArgumentNullException("products");
     }
     //Evaluate products prices
     await _pricingService.EvaluateProductPricesAsync(products);
     //Evaluate discounts
     var promotionContext = WorkContext.ToPromotionEvaluationContext();
     promotionContext.PromoEntries = products.Select(x => x.ToPromotionItem()).ToList();
     await _promotionEvaluator.EvaluateDiscountsAsync(promotionContext, products);
     var retVal = products.Select(x => x.Price).ToArray();
     return Json(retVal, JsonRequestBehavior.AllowGet);
 }
示例#7
0
        public virtual Variant ToLiquidVariant(storefrontModel.Product product)
        {
            var result = new Variant
            {
                Available         = product.IsAvailable,
                Buyable           = product.IsBuyable,
                InStock           = product.IsInStock,
                Barcode           = product.Gtin,
                CatalogId         = product.CatalogId,
                CategoryId        = product.CategoryId,
                Id                = product.Id,
                InventoryPolicy   = product.TrackInventory ? "deny" : "continue",
                InventoryQuantity = product.Inventory != null?Math.Max(0, (product.Inventory.InStockQuantity ?? 0L) - (product.Inventory.ReservedQuantity ?? 0L)) : 0,
                                        Options               = product.VariationProperties.Where(p => !string.IsNullOrEmpty(p.Value)).Select(p => p.Value).ToArray(),
                                        CompareAtPrice        = product.Price.ListPrice.Amount * 100,
                                        CompareAtPriceWithTax = product.Price.ListPriceWithTax.Amount * 100,
                                        Price         = product.Price.ActualPrice.Amount * 100,
                                        PriceWithTax  = product.Price.ActualPriceWithTax.Amount * 100,
                                        Selected      = false,
                                        Sku           = product.Sku,
                                        Title         = product.Name,
                                        Url           = product.Url,
                                        Weight        = product.Weight ?? 0m,
                                        WeightUnit    = product.WeightUnit,
                                        FeaturedImage = product.PrimaryImage?.ToShopifyModel(),

                                        PackageType       = product.PackageType,
                                        Height            = product.Height,
                                        MeasureUnit       = product.MeasureUnit,
                                        Width             = product.Width,
                                        Length            = product.Length,
                                        AvailableQuantity = product.AvailableQuantity
            };



            if (result.FeaturedImage != null)
            {
                result.FeaturedImage.ProductId         = product.Id;
                result.FeaturedImage.AttachedToVariant = true;
                result.FeaturedImage.Variants          = new[] { result };
            }

            return(result);
        }
示例#8
0
        public virtual Product ToLiquidProduct(storefrontModel.Product product)
        {
            var result = new Product();

            result.Id          = product.Id;
            result.CatalogId   = product.CatalogId;
            result.CategoryId  = product.CategoryId;
            result.Description = product.Description;
            result.IsQuotable  = product.IsQuotable;
            result.TaxType     = product.TaxType;

            result.Variants.Add(ToLiquidVariant(product));

            if (product.Variations != null && product.Variations.Any())
            {
                result.Variants.AddRange(product.Variations.Select(x => x.ToVariant()));
                result.Available = product.IsAvailable || product.Variations.Any(v => v.IsAvailable);
                result.Buyable   = product.IsBuyable || product.Variations.Any(v => v.IsBuyable);
                result.InStock   = product.IsInStock || product.Variations.Any(v => v.IsInStock);
            }
            else
            {
                result.Available = product.IsAvailable;
                result.Buyable   = product.IsBuyable;
                result.InStock   = product.IsInStock;
            }

            result.CatalogId  = product.CatalogId;
            result.CategoryId = product.CategoryId;

            result.CompareAtPriceMax    = result.Variants.Select(x => x.CompareAtPrice).Max();
            result.CompareAtPriceMin    = result.Variants.Select(x => x.CompareAtPrice).Min();
            result.CompareAtPriceVaries = result.CompareAtPriceMax != result.CompareAtPriceMin;

            result.CompareAtPrice        = product.Price.ListPrice.Amount * 100;
            result.CompareAtPriceWithTax = product.Price.ListPriceWithTax.Amount * 100;
            result.Price        = product.Price.ActualPrice.Amount * 100;
            result.PriceWithTax = product.Price.ActualPriceWithTax.Amount * 100;

            result.PriceMax    = result.Variants.Select(x => x.Price).Max();
            result.PriceMin    = result.Variants.Select(x => x.Price).Min();
            result.PriceVaries = result.PriceMax != result.PriceMin;

            result.Content     = product.Description;
            result.Description = result.Content;

            result.Descriptions = new Descriptions(product.Descriptions.Select(d => new Description
            {
                Content = d.Value,
                Type    = d.ReviewType
            }));

            result.FeaturedImage = product.PrimaryImage != null?product.PrimaryImage.ToShopifyModel() : null;

            if (result.FeaturedImage != null)
            {
                result.FeaturedImage.ProductId         = product.Id;
                result.FeaturedImage.AttachedToVariant = false;
            }

            result.FirstAvailableVariant = result.Variants.FirstOrDefault(x => x.Available);
            result.Handle = product.SeoInfo != null ? product.SeoInfo.Slug : product.Id;
            result.Images = product.Images.Select(x => x.ToShopifyModel()).ToArray();

            foreach (var image in result.Images)
            {
                image.ProductId         = product.Id;
                image.AttachedToVariant = false;
            }

            if (product.VariationProperties != null)
            {
                result.Options = product.VariationProperties.Where(x => !string.IsNullOrEmpty(x.Value)).Select(x => x.Name).ToArray();
            }

            if (product.Properties != null)
            {
                result.Properties = product.Properties.Select(x => x.ToShopifyModel()).ToList();
                result.Metafields = new MetaFieldNamespacesCollection(new[] { new MetafieldsCollection("properties", product.Properties) });
            }

            result.SelectedVariant = result.Variants.First();
            result.Title           = product.Name;
            result.Type            = product.ProductType;
            result.Url             = product.Url;

            result.PackageType = product.PackageType;
            result.WeightUnit  = product.WeightUnit;
            result.Weight      = product.Weight;
            result.Height      = product.Height;
            result.MeasureUnit = product.MeasureUnit;
            result.Width       = product.Width;
            result.Length      = product.Length;
            result.Outline     = product.Outline;

            if (!product.Associations.IsNullOrEmpty())
            {
                result.RelatedProducts = new MutablePagedList <Product>((pageNumber, pageSize, sortInfos) =>
                {
                    //Need to load related products from associated  product and categories
                    var skip = (pageNumber - 1) * pageSize;
                    var take = pageSize;
                    var productAssociations = product.Associations.OfType <storefrontModel.ProductAssociation>().OrderBy(x => x.Priority);
                    var retVal     = productAssociations.Select(x => x.Product).Skip(skip).Take(take).ToList();
                    var totalCount = productAssociations.Count();
                    skip           = Math.Max(0, skip - totalCount);
                    take           = Math.Max(0, take - retVal.Count);

                    //Load product from associated categories with correct pagination
                    foreach (var categoryAssociation in product.Associations.OfType <storefrontModel.CategoryAssociation>().OrderBy(x => x.Priority))
                    {
                        if (categoryAssociation.Category != null && categoryAssociation.Category.Products != null)
                        {
                            categoryAssociation.Category.Products.Slice(skip / pageSize + 1, take, sortInfos);
                            retVal.AddRange(categoryAssociation.Category.Products);
                            totalCount += categoryAssociation.Category.Products.GetTotalCount();
                            skip        = Math.Max(0, skip - totalCount);
                            take        = Math.Max(0, take - categoryAssociation.Category.Products.Count());
                        }
                    }

                    return(new StaticPagedList <Product>(retVal.Select(x => x.ToShopifyModel()), pageNumber, pageSize, totalCount));
                }, 1, 20);
            }

            if (product.Vendor != null)
            {
                result.Vendor = product.Vendor.ToShopifyModel();
            }
            return(result);
        }
示例#9
0
        public static Variant ToVariant(this storefrontModel.Product product)
        {
            var converter = new ShopifyModelConverter();

            return(converter.ToLiquidVariant(product));
        }
示例#10
0
        public virtual Product ToLiquidProduct(storefrontModel.Product product)
        {
            var result = new Product
            {
                Id          = product.Id,
                CatalogId   = product.CatalogId,
                CategoryId  = product.CategoryId,
                Description = product.Description,
                IsQuotable  = product.IsQuotable,
                TaxType     = product.TaxType
            };

            result.Variants.Add(ToLiquidVariant(product));

            if (product.Variations != null && product.Variations.Any())
            {
                result.Variants.AddRange(product.Variations.Select(x => x.ToVariant()));
                result.Available = product.IsAvailable || product.Variations.Any(v => v.IsAvailable);
                result.Buyable   = product.IsBuyable || product.Variations.Any(v => v.IsBuyable);
                result.InStock   = product.IsInStock || product.Variations.Any(v => v.IsInStock);
            }
            else
            {
                result.Available = product.IsAvailable;
                result.Buyable   = product.IsBuyable;
                result.InStock   = product.IsInStock;
            }

            result.CatalogId  = product.CatalogId;
            result.CategoryId = product.CategoryId;

            result.CompareAtPriceMax    = result.Variants.Select(x => x.CompareAtPrice).Max();
            result.CompareAtPriceMin    = result.Variants.Select(x => x.CompareAtPrice).Min();
            result.CompareAtPriceVaries = result.CompareAtPriceMax != result.CompareAtPriceMin;

            result.CompareAtPrice        = product.Price.ListPrice.Amount * 100;
            result.CompareAtPriceWithTax = product.Price.ListPriceWithTax.Amount * 100;
            result.Price        = product.Price.ActualPrice.Amount * 100;
            result.PriceWithTax = product.Price.ActualPriceWithTax.Amount * 100;

            result.PriceMax    = result.Variants.Select(x => x.Price).Max();
            result.PriceMin    = result.Variants.Select(x => x.Price).Min();
            result.PriceVaries = result.PriceMax != result.PriceMin;

            result.Content     = product.Description;
            result.Description = result.Content;

            result.Descriptions = new Descriptions(product.Descriptions.Select(d => new Description
            {
                Content = d.Value,
                Type    = d.ReviewType
            }));

            result.ProductRatings = product.ProductRatings?.Select(x => x.ToShopifyModel()).ToArray();

            result.FeaturedImage = product.PrimaryImage?.ToShopifyModel();

            if (result.FeaturedImage != null)
            {
                result.FeaturedImage.ProductId         = product.Id;
                result.FeaturedImage.AttachedToVariant = false;
            }

            result.FirstAvailableVariant = result.Variants.FirstOrDefault(x => x.Available);
            result.Handle = product.SeoInfo != null ? product.SeoInfo.Slug : product.Id;
            result.Images = product.Images.Select(x => x.ToShopifyModel()).ToArray();

            foreach (var image in result.Images)
            {
                image.ProductId         = product.Id;
                image.AttachedToVariant = false;
            }

            if (product.VariationProperties != null)
            {
                result.Options = product.VariationProperties.Where(x => !string.IsNullOrEmpty(x.Value)).Select(x => x.Name).ToArray();
            }

            if (product.Properties != null)
            {
                result.Properties = product.Properties.Select(x => x.ToShopifyModel()).ToList();
                result.Metafields = new MetaFieldNamespacesCollection(new[] { new MetafieldsCollection("properties", product.Properties) });
            }

            result.SelectedVariant = result.Variants.First();
            result.Title           = product.Name;
            result.Type            = product.ProductType;
            result.Url             = product.Url;

            result.PackageType = product.PackageType;
            result.WeightUnit  = product.WeightUnit;
            result.Weight      = product.Weight;
            result.Height      = product.Height;
            result.MeasureUnit = product.MeasureUnit;
            result.Width       = product.Width;
            result.Length      = product.Length;
            result.Outline     = product.Outline;

            if (product.Associations != null)
            {
                result.RelatedProducts = new MutablePagedList <Product>((pageNumber, pageSize, sortInfos, @params) =>
                {
                    product.Associations.Slice(pageNumber, pageSize, sortInfos, @params);
                    return(new StaticPagedList <Product>(product.Associations.Where(x => x.Product != null).Select(x => x.Product.ToShopifyModel()), product.Associations));
                }, product.Associations.PageNumber, product.Associations.PageSize);
            }

            if (product.Vendor != null)
            {
                result.Vendor = product.Vendor.ToShopifyModel();
            }
            return(result);
        }
示例#11
0
        public static Variant ToVariant(this storefrontModel.Product product)
        {
            var converter = ServiceLocator.Current.GetInstance <ShopifyModelConverter>();

            return(converter.ToLiquidVariant(product));
        }
 private void LoadProductsInventories(Product[] products)
 {
     var inventories = _inventoryModuleApi.InventoryModuleGetProductsInventories(products.Select(x => x.Id).ToList());
     foreach (var item in products)
     {
         item.Inventory = inventories.Where(x => x.ProductId == item.Id).Select(x => x.ToWebModel()).FirstOrDefault();
     }
 }
        private void LoadProductsPrices(Product[] products)
        {
            var result = _pricingModuleApi.PricingModuleEvaluatePrices(
                   evalContextStoreId: _workContext.CurrentStore.Id,
                   evalContextCatalogId: _workContext.CurrentStore.Catalog, 
                   evalContextProductIds: products.Select(x => x.Id).ToList(),
                   evalContextCustomerId: _workContext.CurrentCustomer.Id,
                   evalContextCertainDate: _workContext.StorefrontUtcNow,
                   evalContextPricelistIds: _workContext.CurrentPriceListIds.ToList());

            foreach (var item in products)
            {
                item.Prices = result.Where(x => x.ProductId == item.Id).Select(x => x.ToWebModel()).ToList();
                var price = item.Prices.FirstOrDefault(x => x.Currency.Equals(_workContext.CurrentCurrency));
                if (price != null)
                {
                    item.Price = price;
                }
            }
        }
 private async Task LoadProductsDiscountsAsync(Product[] products)
 {
     var promotionContext = _workContext.ToPromotionEvaluationContext();
     promotionContext.PromoEntries = products.Select(x=>x.ToPromotionItem()).ToList();
     await _promotionEvaluator.EvaluateDiscountsAsync(promotionContext, products);
 }