private PictureModel PrepareOrderItemPictureModel( Product product, int pictureSize, string productName, string attributesXml, CatalogSettings catalogSettings) { Guard.NotNull(product, nameof(product)); MediaFileInfo file = null; var combination = _productAttributeParser.FindProductVariantAttributeCombination(product.Id, attributesXml); if (combination != null) { var mediaIds = combination.GetAssignedMediaIds(); if (mediaIds != null && mediaIds.Length > 0) { file = _mediaService.GetFileById(mediaIds[0], MediaLoadFlags.AsNoTracking); } } // No attribute combination image, then load product picture. if (file == null) { var mediaFile = _productService.GetProductPicturesByProductId(product.Id, 1) .Select(x => x.MediaFile) .FirstOrDefault(); if (mediaFile != null) { file = _mediaService.ConvertMediaFile(mediaFile); } } if (file == null && product.Visibility == ProductVisibility.Hidden && product.ParentGroupedProductId > 0) { // Let's check whether this product has some parent "grouped" product. var mediaFile = _productService.GetProductPicturesByProductId(product.ParentGroupedProductId, 1) .Select(x => x.MediaFile) .FirstOrDefault(); if (mediaFile != null) { file = _mediaService.ConvertMediaFile(mediaFile); } } return(new PictureModel { PictureId = file?.Id ?? 0, Size = pictureSize, ImageUrl = _mediaService.GetUrl(file, pictureSize, null, !catalogSettings.HideProductDefaultPictures), Title = file?.File?.GetLocalized(x => x.Title)?.Value.NullEmpty() ?? T("Media.Product.ImageLinkTitleFormat", productName), AlternateText = file?.File?.GetLocalized(x => x.Alt)?.Value.NullEmpty() ?? T("Media.Product.ImageAlternateTextFormat", productName), File = file }); }
/// <summary> /// Gets the shopping cart unit price (one item) /// </summary> /// <param name="shoppingCartItem">The shopping cart item</param> /// <param name="includeDiscounts">A value indicating whether include discounts or not for price computation</param> /// <returns>Shopping cart unit price (one item)</returns> public virtual decimal GetUnitPrice(ShoppingCartItem shoppingCartItem, bool includeDiscounts) { if (shoppingCartItem == null) { throw new ArgumentNullException("shoppingCartItem"); } var customer = shoppingCartItem.Customer; decimal finalPrice = decimal.Zero; var product = shoppingCartItem.Product; if (product != null) { var combination = _productAttributeParser.FindProductVariantAttributeCombination(product, shoppingCartItem.AttributesXml); if (combination != null && combination.OverriddenPrice.HasValue) { finalPrice = combination.OverriddenPrice.Value; } else { //summarize price of all attributes decimal attributesTotalPrice = decimal.Zero; var pvaValues = _productAttributeParser.ParseProductVariantAttributeValues(shoppingCartItem.AttributesXml); if (pvaValues != null) { foreach (var pvaValue in pvaValues) { attributesTotalPrice += GetProductVariantAttributeValuePriceAdjustment(pvaValue); } } //get price of a product (with previously calculated price of all attributes) if (product.CustomerEntersPrice) { finalPrice = shoppingCartItem.CustomerEnteredPrice; } else { finalPrice = GetFinalPrice(product, customer, attributesTotalPrice, includeDiscounts, shoppingCartItem.Quantity); } } } //rounding if (_shoppingCartSettings.RoundPricesDuringCalculation) { finalPrice = Math.Round(finalPrice, 2); } return(finalPrice); }
/// <summary> /// Gets the shopping cart unit price (one item) /// </summary> /// <param name="shoppingCartItem">The shopping cart item</param> /// <param name="includeDiscounts">A value indicating whether include discounts or not for price computation</param> /// <returns>Shopping cart unit price (one item)</returns> public virtual decimal GetUnitPrice(OrganizedShoppingCartItem shoppingCartItem, bool includeDiscounts) { decimal finalPrice = decimal.Zero; var customer = shoppingCartItem.Item.Customer; var product = shoppingCartItem.Item.Product; if (product != null) { if (product.CustomerEntersPrice) { finalPrice = shoppingCartItem.Item.CustomerEnteredPrice; } else if (product.ProductType == ProductType.BundledProduct && product.BundlePerItemPricing) { if (shoppingCartItem.ChildItems != null) { var bundleItems = shoppingCartItem.ChildItems.Where(x => x.BundleItemData.IsValid()).Select(x => x.BundleItemData).ToList(); finalPrice = GetFinalPrice(product, bundleItems, customer, decimal.Zero, includeDiscounts, shoppingCartItem.Item.Quantity); } } else { var combination = _productAttributeParser.FindProductVariantAttributeCombination(product, shoppingCartItem.Item.AttributesXml); if (combination != null && combination.Price.HasValue) { finalPrice = combination.Price.Value; } else { decimal attributesTotalPrice = decimal.Zero; var pvaValues = _productAttributeParser.ParseProductVariantAttributeValues(shoppingCartItem.Item.AttributesXml); if (pvaValues != null) { foreach (var pvaValue in pvaValues) { attributesTotalPrice += GetProductVariantAttributeValuePriceAdjustment(pvaValue); } } finalPrice = GetFinalPrice(product, customer, attributesTotalPrice, includeDiscounts, shoppingCartItem.Item.Quantity, shoppingCartItem.BundleItemData); } } } if (_shoppingCartSettings.RoundPricesDuringCalculation) { finalPrice = Math.Round(finalPrice, 2); } return(finalPrice); }
public static ProductVariantAttributeCombination MergeWithCombination(this Product product, string selectedAttributes, IProductAttributeParser productAttributeParser) { Guard.NotNull(productAttributeParser, "productAttributeParser"); if (selectedAttributes.IsEmpty()) { return(null); } // let's find appropriate record var combination = productAttributeParser.FindProductVariantAttributeCombination(product.Id, selectedAttributes); if (combination != null && combination.IsActive) { product.MergeWithCombination(combination); } return(combination); }
private PictureModel PrepareOrderItemPictureModel( Product product, int pictureSize, string productName, string attributesXml, CatalogSettings catalogSettings) { Guard.NotNull(product, nameof(product)); Picture picture = null; var combination = _productAttributeParser.FindProductVariantAttributeCombination(product.Id, attributesXml); if (combination != null) { var picturesIds = combination.GetAssignedPictureIds(); if (picturesIds != null && picturesIds.Length > 0) { picture = _pictureService.GetPictureById(picturesIds[0]); } } // No attribute combination image, then load product picture if (picture == null) { picture = _pictureService.GetPicturesByProductId(product.Id, 1).FirstOrDefault(); } if (picture == null && product.Visibility == ProductVisibility.Hidden && product.ParentGroupedProductId > 0) { // Let's check whether this product has some parent "grouped" product picture = _pictureService.GetPicturesByProductId(product.ParentGroupedProductId, 1).FirstOrDefault(); } return(new PictureModel { PictureId = picture != null ? picture.Id : 0, Size = pictureSize, ImageUrl = _pictureService.GetUrl(picture, pictureSize, !catalogSettings.HideProductDefaultPictures), Title = T("Media.Product.ImageLinkTitleFormat", productName), AlternateText = T("Media.Product.ImageAlternateTextFormat", productName) }); }
public virtual AdjustInventoryResult AdjustInventory(Product product, bool decrease, int quantity, string attributesXml) { Guard.NotNull(product, nameof(product)); var result = new AdjustInventoryResult(); switch (product.ManageInventoryMethod) { case ManageInventoryMethod.DontManageStock: { //do nothing } break; case ManageInventoryMethod.ManageStock: { result.StockQuantityOld = product.StockQuantity; if (decrease) { result.StockQuantityNew = product.StockQuantity - quantity; } else { result.StockQuantityNew = product.StockQuantity + quantity; } bool newPublished = product.Published; bool newDisableBuyButton = product.DisableBuyButton; bool newDisableWishlistButton = product.DisableWishlistButton; //check if minimum quantity is reached switch (product.LowStockActivity) { case LowStockActivity.DisableBuyButton: newDisableBuyButton = product.MinStockQuantity >= result.StockQuantityNew; newDisableWishlistButton = product.MinStockQuantity >= result.StockQuantityNew; break; case LowStockActivity.Unpublish: newPublished = product.MinStockQuantity <= result.StockQuantityNew; break; } product.StockQuantity = result.StockQuantityNew; product.DisableBuyButton = newDisableBuyButton; product.DisableWishlistButton = newDisableWishlistButton; product.Published = newPublished; UpdateProduct(product); //send email notification if (decrease && product.NotifyAdminForQuantityBelow > result.StockQuantityNew) { _services.MessageFactory.SendQuantityBelowStoreOwnerNotification(product, _localizationSettings.DefaultAdminLanguageId); } } break; case ManageInventoryMethod.ManageStockByAttributes: { var combination = _productAttributeParser.FindProductVariantAttributeCombination(product.Id, attributesXml); if (combination != null) { result.StockQuantityOld = combination.StockQuantity; if (decrease) { result.StockQuantityNew = combination.StockQuantity - quantity; } else { result.StockQuantityNew = combination.StockQuantity + quantity; } combination.StockQuantity = result.StockQuantityNew; _productAttributeService.UpdateProductVariantAttributeCombination(combination); } } break; default: break; } var attributeValues = _productAttributeParser.ParseProductVariantAttributeValues(attributesXml); attributeValues .Where(x => x.ValueType == ProductVariantAttributeValueType.ProductLinkage) .ToList() .Each(x => { var linkedProduct = GetProductById(x.LinkedProductId); if (linkedProduct != null) { AdjustInventory(linkedProduct, decrease, quantity * x.Quantity, ""); } }); return(result); }
/// <summary> /// Gets the shopping cart unit price (one item) /// </summary> /// <param name="shoppingCartItem">The shopping cart item</param> /// <param name="includeDiscounts">A value indicating whether include discounts or not for price computation</param> /// <returns>Shopping cart unit price (one item)</returns> public virtual decimal GetUnitPrice(ShoppingCartItem shoppingCartItem, bool includeDiscounts) { if (shoppingCartItem == null) { throw new ArgumentNullException("shoppingCartItem"); } var customer = shoppingCartItem.Customer; decimal finalPrice = decimal.Zero; var product = shoppingCartItem.Product; if (product != null) { var combination = _productAttributeParser.FindProductVariantAttributeCombination(product, shoppingCartItem.AttributesXml); if (combination != null && combination.OverriddenPrice.HasValue) { finalPrice = combination.OverriddenPrice.Value; } else { //summarize price of all attributes decimal attributesTotalPrice = decimal.Zero; var pvaValues = _productAttributeParser.ParseProductVariantAttributeValues(shoppingCartItem.AttributesXml); if (pvaValues != null) { foreach (var pvaValue in pvaValues) { attributesTotalPrice += GetProductVariantAttributeValuePriceAdjustment(pvaValue); } } //get price of a product (with previously calculated price of all attributes) if (product.CustomerEntersPrice) { finalPrice = shoppingCartItem.CustomerEnteredPrice; } else { var qty = 0; if (_shoppingCartSettings.GroupTierPricesForDistinctShoppingCartItems) { //the same products with distinct product attributes could be stored as distinct "ShoppingCartItem" records //so let's find how many of the current products are in the cart qty = customer.ShoppingCartItems .Where(x => x.ProductId == shoppingCartItem.ProductId) .Where(x => x.ShoppingCartTypeId == shoppingCartItem.ShoppingCartTypeId) .Sum(x => x.Quantity); if (qty == 0) { qty = shoppingCartItem.Quantity; } } else { qty = shoppingCartItem.Quantity; } finalPrice = GetFinalPrice(product, customer, attributesTotalPrice, includeDiscounts, qty); } } } //rounding if (_shoppingCartSettings.RoundPricesDuringCalculation) { finalPrice = Math.Round(finalPrice, 2); } return(finalPrice); }
public static ProductVariantAttributeCombination MergeWithCombination(this Product product, string selectedAttributes, IProductAttributeParser productAttributeParser) { Guard.ArgumentNotNull(productAttributeParser, "productAttributeParser"); if (selectedAttributes.IsEmpty()) return null; // let's find appropriate record var combination = productAttributeParser.FindProductVariantAttributeCombination(product.Id, selectedAttributes); if (combination != null && combination.IsActive) { product.MergeWithCombination(combination); } return combination; }