예제 #1
0
        /// <summary>
        /// Adds to shopping cart.
        /// </summary>
        /// <param name="productCode">The product code.</param>
        /// <param name="quantity">The quantity.</param>
        public virtual void AddProduct(string productCode, uint quantity)
        {
            Assert.ArgumentNotNullOrEmpty(productCode, "productCode");
            if (quantity == 0)
            {
                return;
            }

            DomainModel.Carts.ShoppingCartLine existingShoppingCartLine = this.shoppingCart.ShoppingCartLines.FirstOrDefault(p => p.Product.Code.Equals(productCode));

            if (existingShoppingCartLine != null)
            {
                existingShoppingCartLine.Quantity += quantity;
                existingShoppingCartLine.Totals    = this.productPriceManager.GetProductTotals <Totals, ProductBaseData, Currency>(existingShoppingCartLine.Product, this.shoppingCart.Currency, existingShoppingCartLine.Quantity);
            }
            else
            {
                existingShoppingCartLine = Context.Entity.Resolve <DomainModel.Carts.ShoppingCartLine>();

                ProductBaseData product = this.productRepository.Get <ProductBaseData>(productCode);
                Assert.IsNotNull(product, "Unable to add product \"{0}\" to the shopping cart. The product is not found.", productCode);

                existingShoppingCartLine.Product  = product;
                existingShoppingCartLine.Quantity = quantity;

                existingShoppingCartLine.Totals = this.productPriceManager.GetProductTotals <Totals, ProductBaseData, Currency>(product, this.shoppingCart.Currency, existingShoppingCartLine.Quantity);

                this.shoppingCart.ShoppingCartLines.Add(existingShoppingCartLine);
            }

            Context.Entity.SetInstance(this.shoppingCart);
        }
예제 #2
0
        /// <summary>
        /// Inserts the specified parent item.
        /// </summary>
        /// <typeparam name="T">The type of the product repository item.</typeparam>
        /// <param name="parentItem">The parent item.</param>
        /// <param name="item">The needed item.</param>
        /// <exception cref="ArgumentException">Product repository item title is invalid.</exception>
        protected virtual void Insert <T>(Item parentItem, T item) where T : IProductRepositoryItem
        {
            Assert.ArgumentNotNull(parentItem, "parentItem");
            Assert.ArgumentNotNull(item, "item");

            this.CheckDuplicatedCode(item.Code);

            Item newItem;

            ID templateId = null;
            ITemplatedEntity templatedEntity = item as ITemplatedEntity;

            if (templatedEntity != null && !string.IsNullOrEmpty(templatedEntity.Template))
            {
                Item templateItem = this.Database.GetItem(templatedEntity.Template);
                templateId = templateItem.ID;
            }

            try
            {
                if (ID.IsNullOrEmpty(templateId))
                {
                    templateId = this.GetRepositoryItemTemplateId(typeof(T));
                }

                newItem = parentItem.Add(item.Name, new TemplateID(templateId));
            }
            catch (InvalidItemNameException exception)
            {
                Log.Error("Product title is invalid.", exception, this);
                throw new ArgumentException("Product repository item title is invalid.", exception);
            }
            catch (Exception exception)
            {
                Log.Error("Failed to create product item", exception, this);
                throw;
            }

            Assert.IsNotNull(newItem, "Failed to create product item.");

            Assert.IsNotNull(this.DataMapper, "DataMapper cannot be null.");
            this.DataMapper.SaveEntity(item, newItem);

            ProductBaseData product = item as ProductBaseData;

            if (product == null)
            {
                return;
            }

            using (new EditContext(newItem))
            {
                foreach (KeyValuePair <string, object> specification in product.Specifications)
                {
                    newItem[specification.Key] = specification.Value.ToString();
                }
            }
        }
예제 #3
0
        public virtual double?GetAverageRating(ProductBaseData product)
        {
            var reviews = ProductReviewRepository.GetByProduct(product.Code).Where(x => x.Approved && !x.Hidden);

            if (reviews.Any())
            {
                return(reviews.Average(x => x.Rating));
            }
            return(null);
        }
예제 #4
0
        protected override object ComputeFieldValue(ProductBaseData product, IIndexable indexable)
        {
            var pricing = product.GetDisplayTotals();

            if (pricing == null)
            {
                return(null);
            }
            return(pricing.MinPrice);
        }
        public override ProductSpecification ConvertFrom([NotNull] Item storage)
        {
            Assert.ArgumentNotNull(storage, "storage");

            ProductBaseData      product       = this.productFactory.Create(storage.TemplateID.ToString());
            ProductSpecification specification = product.Specifications;

            foreach (string key in specification.Keys.ToArray())
            {
                specification[key] = storage[key];
            }

            return(specification);
        }
예제 #6
0
        protected override object ComputeFieldValue(ProductBaseData product, IIndexable indexable)
        {
            var fullProduct = product as Product;

            if (fullProduct == null)
            {
                return(null);
            }
            var image = fullProduct.Image;

            if (image == null)
            {
                return(null);
            }
            return(image.GetUri(166, 166));
        }
예제 #7
0
        private ProductLineModel GetProductLineModel([NotNull] ProductBaseData productBaseData)
        {
            Assert.ArgumentNotNull(productBaseData, "productBaseData");

            long stock = this.ProductStockManager.GetStock(new ProductStockInfo
            {
                ProductCode = productBaseData.Code
            }).Stock;

            var price = this.PriceService.GetPrice(productBaseData, this.currencyCode);

            return(new ProductLineModel
            {
                Code = productBaseData.Code,
                Name = productBaseData.Name,
                Stock = stock,
                Price = price
            });
        }
예제 #8
0
        /// <summary>
        /// Gets the general price.
        /// </summary>
        /// <param name="ni">The ni.</param>
        /// <returns>The general price.</returns>
        private Totals GetProductTotals(XPathNodeIterator ni)
        {
            Item item = this.GetItem(ni);

            Assert.ArgumentNotNull(item, "item");

            IProductRepository productProvider = Context.Entity.Resolve <IProductRepository>();

            ProductBaseData product = productProvider.Get <ProductBaseData>(item["Product Code"]);

            if (product != null)
            {
                ShoppingCart         shoppingCart        = Context.Entity.GetInstance <ShoppingCart>();
                IProductPriceManager productPriceManager = Context.Entity.Resolve <IProductPriceManager>();
                Totals productPricesList = productPriceManager.GetProductTotals <Totals, ProductBaseData, Currency>(product, shoppingCart.Currency);

                return(productPricesList);
            }

            return(default(Totals));
        }
예제 #9
0
        public override ProductBaseData Create([NotNull] string template)
        {
            Assert.ArgumentNotNull(template, "template");

            Collection <string> spec = new Collection <string>();

            Assert.IsNotNull(this.ShopContext, "Unable to create a product. Context shop cannot be null.");
            Assert.IsNotNull(this.ShopContext.Database, "Unable to create a product. Context shop database cannot be null.");

            TemplateItem templateItem = this.ShopContext.Database.GetItem(template);

            Assert.IsNotNull(templateItem, "Product template is not found.");

            this.FillSpecificationKeys(templateItem, ref spec);

            ProductBaseData product = Context.Entity.SmartResolve <ProductBaseData>(template);

            product.Specifications = new ProductSpecification(spec);
            ((ITemplatedEntity)product).Template = template;

            return(product);
        }
예제 #10
0
        public virtual decimal?GetPrice([NotNull] ProductBaseData product, [NotNull] string priceCode)
        {
            Assert.ArgumentNotNull(product, "product");
            Assert.ArgumentNotNull(priceCode, "priceCode");

            var currency = this.CurrencyProvider.Get(priceCode);

            decimal?price = null;

            if (currency != null)
            {
                try
                {
                    Totals totals = this.ProductPriceManager.GetProductTotals <Totals, ProductBaseData, Currency>(product, currency);
                    price = Math.Round(totals.PriceExVat, 2, MidpointRounding.AwayFromZero);
                }
                catch (CurrencyConversionException)
                {
                    price = null;
                }
            }

            return(price);
        }
예제 #11
0
        protected virtual double GetRating(ProductBaseData product)
        {
            var average = product.GetAverageRating();

            return(average ?? double.MinValue);
        }
예제 #12
0
 public static double?GetAverageRating(this ProductBaseData product)
 {
     return(Instance.GetAverageRating(product));
 }
 protected abstract object ComputeFieldValue(ProductBaseData product, Sitecore.ContentSearch.IIndexable indexable);