Пример #1
0
        public override ManagerResponse <GetProductBulkPricesResult, bool> GetProductBulkPrices(CommerceStorefront storefront, IVisitorContext visitorContext, IEnumerable <ProductEntity> productEntityList, StringPropertyCollection propertyBag = null)
        {
            if (productEntityList == null || !productEntityList.Any())
            {
                return(new ManagerResponse <GetProductBulkPricesResult, bool>(new GetProductBulkPricesResult(), true));
            }
            string        catalogName = productEntityList.Select(p => p.CatalogName).FirstOrDefault();
            List <string> list        = productEntityList.Select(p => p.ProductId + (p.Variants != null && p.Variants.Any()? "|" + p.Variants.First().VariantId : "")).ToList();
            ManagerResponse <GetProductBulkPricesResult, IDictionary <string, Price> > productBulkPrices = this.PricingManager.GetProductBulkPrices(storefront, visitorContext, catalogName, list, propertyBag, null);
            IDictionary <string, Price> source = productBulkPrices == null || productBulkPrices.Result == null ? new Dictionary <string, Price>() : productBulkPrices.Result;

            foreach (ProductEntity productEntity in productEntityList)
            {
                Price price;
                if (source.Any() && source.TryGetValue(productEntity.ProductId, out price))
                {
                    CommercePrice commercePrice = (CommercePrice)price;
                    productEntity.ListPrice     = commercePrice.LowestPricedVariantListPrice.HasValue? commercePrice.LowestPricedVariantListPrice.Value : new decimal?(commercePrice.Amount);
                    productEntity.AdjustedPrice = commercePrice.LowestPricedVariant.HasValue? commercePrice.LowestPricedVariant : new decimal?(commercePrice.ListPrice);
                    productEntity.LowestPricedVariantAdjustedPrice  = commercePrice.LowestPricedVariant;
                    productEntity.LowestPricedVariantListPrice      = commercePrice.LowestPricedVariantListPrice;
                    productEntity.HighestPricedVariantAdjustedPrice = commercePrice.HighestPricedVariant;
                }
            }
            return(new ManagerResponse <GetProductBulkPricesResult, bool>(new GetProductBulkPricesResult(), true));
        }
Пример #2
0
        public ManagerResponse <GetProductBulkPricesResult, bool> GetProductBulkPrices(List <Product> products)
        {
            if (products == null || !products.Any())
            {
                return(new ManagerResponse <GetProductBulkPricesResult, bool>(new GetProductBulkPricesResult(), true));
            }

            string catalogName                 = products.Select((p => p.CatalogName)).First().ToString();
            var    productIds                  = products.Select((p => p.ProductId));
            var    productBulkPrices           = this.pricingManager.GetProductBulkPrices(catalogName, productIds, null);
            IDictionary <string, Price> source = productBulkPrices == null || productBulkPrices.Result == null ? new Dictionary <string, Price>() : productBulkPrices.Result;

            foreach (Product product in products)
            {
                Price price;
                if (source.Any() && source.TryGetValue(product.ProductId, out price))
                {
                    CommercePrice commercePrice = (CommercePrice)price;
                    product.CurrencyCode  = price.CurrencyCode;
                    product.ListPrice     = new Decimal?(commercePrice.Amount);
                    product.AdjustedPrice = new Decimal?(commercePrice.ListPrice);
                }
            }
            return(new ManagerResponse <GetProductBulkPricesResult, bool>(new GetProductBulkPricesResult(), true));
        }
        protected override void TranslateProduct(TranslateCartLineToEntityRequest request, CartLineComponent source, CommerceCartLine destination, bool isSubLine = false)
        {
            Assert.ArgumentNotNull((object)request, nameof(request));
            Assert.ArgumentNotNull((object)source, nameof(source));
            Assert.ArgumentNotNull((object)destination, nameof(destination));
            CommerceCartProduct commerceCartProduct = this.EntityFactory.Create <CommerceCartProduct>("CartProduct");

            if (source.CartLineComponents != null && !string.IsNullOrEmpty(source.ItemId))
            {
                CartProductComponent productComponent = source.CartLineComponents.OfType <CartProductComponent>().FirstOrDefault <CartProductComponent>();
                if (productComponent != null)
                {
                    string[] strArray = source.ItemId.Split("|".ToCharArray());
                    commerceCartProduct.ProductCatalog        = strArray[0];
                    commerceCartProduct.ProductId             = productComponent.Id;
                    commerceCartProduct.DisplayName           = productComponent.DisplayName;
                    commerceCartProduct.ProductName           = string.IsNullOrEmpty(productComponent.ProductName) ? productComponent.DisplayName : productComponent.ProductName;
                    commerceCartProduct.SitecoreProductItemId = this.GetSitecoreItemId(strArray[1], strArray[2]);
                    destination.SetPropertyValue("_product_Images", productComponent.Image == null || string.IsNullOrEmpty(productComponent.Image.SitecoreId) ? (object)string.Empty : (object)productComponent.Image.SitecoreId);
                    commerceCartProduct.SetPropertyValue("Color", string.IsNullOrEmpty(productComponent.Color) ? (object)(string)null : (object)productComponent.Color);
                    commerceCartProduct.SetPropertyValue("Size", string.IsNullOrEmpty(productComponent.Size) ? (object)(string)null : (object)productComponent.Size);
                    commerceCartProduct.SetPropertyValue("Style", string.IsNullOrEmpty(productComponent.Style) ? (object)(string)null : (object)productComponent.Style);
                    ID result;
                    if (!string.IsNullOrEmpty(productComponent.ExternalId) && ID.TryParse(productComponent.ExternalId, out result))
                    {
                        commerceCartProduct.SitecoreProductItemId = result.ToGuid();
                    }
                }
                ItemVariationSelectedComponent selectedComponent = source.CartLineComponents.OfType <ItemVariationSelectedComponent>().FirstOrDefault <ItemVariationSelectedComponent>();
                if (selectedComponent != null)
                {
                    commerceCartProduct.ProductVariantId = selectedComponent.VariationId;
                }

                //Set an additional property to determine the Promotion Awarding Blocks [Promotion Plugin Issue#2 described in Known Issues with Promotion plugin blog post]
                if (source.Adjustments != null && source.Adjustments.Any())
                {
                    destination.SetPropertyValue(CartAdjustmentTypePropertyName, string.Join("|", source.Adjustments.Select(x => x.AwardingBlock)));
                }
            }
            CommercePrice commercePrice = this.EntityFactory.Create <CommercePrice>("Price");

            if (source.UnitListPrice != null)
            {
                PurchaseOptionMoneyPolicy optionMoneyPolicy = source.Policies.OfType <PurchaseOptionMoneyPolicy>().FirstOrDefault <PurchaseOptionMoneyPolicy>();
                if (optionMoneyPolicy != null && source.UnitListPrice.Amount != optionMoneyPolicy.SellPrice.Amount)
                {
                    commercePrice.CurrencyCode = optionMoneyPolicy.SellPrice.CurrencyCode;
                    commercePrice.ListPrice    = optionMoneyPolicy.SellPrice.Amount;
                }
                else
                {
                    commercePrice.CurrencyCode = source.UnitListPrice.CurrencyCode;
                    commercePrice.ListPrice    = source.UnitListPrice.Amount;
                }
                commercePrice.Amount = commercePrice.ListPrice;
            }
            commerceCartProduct.Price = (Price)commercePrice;
            destination.Product       = (CartProduct)commerceCartProduct;
        }
Пример #4
0
        public void GetProductPrice(Product product)
        {
            if (product == null)
            {
                return;
            }

            bool includeVariants = product.Variants != null && product.Variants.Count > 0;
            ManagerResponse <GetProductPricesResult, IDictionary <string, Price> > productPrices = this.pricingManager.GetProductPrices(product.CatalogName, product.ProductId, includeVariants, null);

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

            if (productPrices.Result.TryGetValue(product.ProductId, out Price price))
            {
                var commercePrice = (CommercePrice)price;
                product.CurrencyCode  = price.CurrencyCode;
                product.ListPrice     = price.Amount;
                product.AdjustedPrice = commercePrice.ListPrice;
            }

            if (!includeVariants)
            {
                return;
            }

            foreach (Variant variant in product.Variants)
            {
                if (productPrices.Result.TryGetValue(variant.VariantId, out price))
                {
                    CommercePrice commercePrice = (CommercePrice)price;
                    variant.CurrencyCode  = commercePrice.CurrencyCode;
                    variant.ListPrice     = commercePrice.Amount;
                    variant.AdjustedPrice = commercePrice.ListPrice;
                }
            }
        }