/// <summary> /// Gets the base price info for a product. /// </summary> /// <param name="priceCalculationService">Price calculation service.</param> /// <param name="product">The product to get the base price info for.</param> /// <param name="options">Price calculation options. The default options are used if <c>null</c>.</param> /// <returns>Base price info.</returns> public static async Task <string> GetBasePriceInfoAsync(this IPriceCalculationService priceCalculationService, Product product, PriceCalculationOptions options = null) { Guard.NotNull(priceCalculationService, nameof(priceCalculationService)); Guard.NotNull(product, nameof(product)); if (!product.BasePriceHasValue || product.BasePriceAmount == 0) { return(string.Empty); } options ??= priceCalculationService.CreateDefaultOptions(false); var context = new PriceCalculationContext(product, options); var price = await priceCalculationService.CalculatePriceAsync(context); return(priceCalculationService.GetBasePriceInfo(product, price.FinalPrice, options.TargetCurrency)); }
/// <summary> /// Calculates the price adjustments of product attributes, usually <see cref="ProductVariantAttributeValue.PriceAdjustment"/>. /// Typically used to display price adjustments of selected attributes on the cart page. /// The calculated adjustment is always a unit price. /// </summary> /// <param name="priceCalculationService">Price calculation service.</param> /// <param name="product">The product.</param> /// <param name="selection">Attribute selection. If <c>null</c> then the price adjustments of all attributes of <paramref name="product"/> are determined.</param> /// <param name="quantity"> /// The product quantity. May have impact on the price, e.g. if tier prices are applied to price adjustments. /// Note that the calculated price is always the unit price. /// </param> /// <param name="options">Price calculation options. The default options are used if <c>null</c>.</param> /// <returns>Price adjustments of selected attributes. Key: <see cref="ProductVariantAttributeValue.Id"/>, value: attribute price adjustment.</returns> public static async Task <IDictionary <int, CalculatedPriceAdjustment> > CalculateAttributePriceAdjustmentsAsync( this IPriceCalculationService priceCalculationService, Product product, ProductVariantAttributeSelection selection = null, int quantity = 1, PriceCalculationOptions options = null) { Guard.NotNull(priceCalculationService, nameof(priceCalculationService)); options ??= priceCalculationService.CreateDefaultOptions(false); var context = new PriceCalculationContext(product, quantity, options); context.Options.DeterminePriceAdjustments = true; context.Options.TaxFormat = null; context.AddSelectedAttributes(selection, product.Id); var price = await priceCalculationService.CalculatePriceAsync(context); return(price.AttributePriceAdjustments.ToDictionarySafe(x => x.AttributeValue.Id)); }
public override async Task MapAsync(OrganizedShoppingCartItem from, TModel to, dynamic parameters = null) { Guard.NotNull(from, nameof(from)); Guard.NotNull(to, nameof(to)); var item = from.Item; var product = from.Item.Product; var customer = item.Customer; var currency = _services.WorkContext.WorkingCurrency; var shoppingCartType = item.ShoppingCartType; var productSeName = await product.GetActiveSlugAsync(); var taxFormat = parameters?.TaxFormat as string; var batchContext = parameters?.BatchContext as ProductBatchContext; await _productAttributeMaterializer.MergeWithCombinationAsync(product, item.AttributeSelection); // General model data. to.Id = item.Id; to.Sku = product.Sku; to.ProductId = product.Id; to.ProductName = product.GetLocalized(x => x.Name); to.ProductSeName = productSeName; to.ProductUrl = await ProductUrlHelper.GetProductUrlAsync(productSeName, from); to.EnteredQuantity = item.Quantity; to.MinOrderAmount = product.OrderMinimumQuantity; to.MaxOrderAmount = product.OrderMaximumQuantity; to.QuantityStep = product.QuantityStep > 0 ? product.QuantityStep : 1; to.ShortDesc = product.GetLocalized(x => x.ShortDescription); to.ProductType = product.ProductType; to.VisibleIndividually = product.Visibility != ProductVisibility.Hidden; to.CreatedOnUtc = item.UpdatedOnUtc; if (item.BundleItem != null) { to.BundleItem.Id = item.BundleItem.Id; to.BundleItem.DisplayOrder = item.BundleItem.DisplayOrder; to.BundleItem.HideThumbnail = item.BundleItem.HideThumbnail; to.BundlePerItemPricing = item.BundleItem.BundleProduct.BundlePerItemPricing; to.BundlePerItemShoppingCart = item.BundleItem.BundleProduct.BundlePerItemShoppingCart; to.AttributeInfo = await ProductAttributeFormatter.FormatAttributesAsync( item.AttributeSelection, product, customer, includePrices : false, includeGiftCardAttributes : true, includeHyperlinks : true, batchContext : batchContext); var bundleItemName = item.BundleItem.GetLocalized(x => x.Name); if (bundleItemName.Value.HasValue()) { to.ProductName = bundleItemName; } var bundleItemShortDescription = item.BundleItem.GetLocalized(x => x.ShortDescription); if (bundleItemShortDescription.Value.HasValue()) { to.ShortDesc = bundleItemShortDescription; } var calculationOptions = _priceCalculationService.CreateDefaultOptions(false, customer, null, batchContext); var calculationContext = await _priceCalculationService.CreateCalculationContextAsync(from, calculationOptions); var(bundleItemUnitPrice, bundleItemSubtotal) = await _priceCalculationService.CalculateSubtotalAsync(calculationContext); if (to.BundlePerItemPricing && to.BundlePerItemShoppingCart) { to.BundleItem.PriceWithDiscount = bundleItemSubtotal.FinalPrice.ToString(); } to.BasePrice = _priceCalculationService.GetBasePriceInfo(product, bundleItemUnitPrice.FinalPrice); } else { to.AttributeInfo = await ProductAttributeFormatter.FormatAttributesAsync(item.AttributeSelection, product, customer, batchContext : batchContext); } var allowedQuantities = product.ParseAllowedQuantities(); foreach (var quantity in allowedQuantities) { to.AllowedQuantities.Add(new SelectListItem { Text = quantity.ToString(), Value = quantity.ToString(), Selected = item.Quantity == quantity }); } var quantityUnit = await Db.QuantityUnits.GetQuantityUnitByIdAsync(product.QuantityUnitId ?? 0, _catalogSettings.ShowDefaultQuantityUnit); if (quantityUnit != null) { to.QuantityUnitName = quantityUnit.GetLocalized(x => x.Name); } if (product.IsRecurring) { to.RecurringInfo = T("ShoppingCart.RecurringPeriod", product.RecurringCycleLength, product.RecurringCyclePeriod.GetLocalizedEnum()); } if (product.CallForPrice) { to.UnitPrice = to.UnitPrice.WithPostFormat(T("Products.CallForPrice")); to.SubTotal = to.UnitPrice; } else if (item.BundleItem == null) { if (shoppingCartType == ShoppingCartType.ShoppingCart) { var subtotal = parameters?.CartSubtotal as ShoppingCartSubtotal; var lineItem = subtotal.LineItems.FirstOrDefault(x => x.Item.Item.Id == item.Id); var unitPrice = CurrencyService.ConvertFromPrimaryCurrency(lineItem.UnitPrice.FinalPrice.Amount, currency); to.UnitPrice = unitPrice.WithPostFormat(taxFormat); var itemSubtotal = CurrencyService.ConvertFromPrimaryCurrency(lineItem.Subtotal.FinalPrice.Amount, currency); to.SubTotal = itemSubtotal.WithPostFormat(taxFormat); if (lineItem.Subtotal.DiscountAmount > 0) { var itemDiscount = CurrencyService.ConvertFromPrimaryCurrency(lineItem.Subtotal.DiscountAmount.Amount, currency); to.Discount = itemDiscount.WithPostFormat(taxFormat); } to.BasePrice = _priceCalculationService.GetBasePriceInfo(product, unitPrice); } else { var calculationOptions = _priceCalculationService.CreateDefaultOptions(false, customer, null, batchContext); var calculationContext = await _priceCalculationService.CreateCalculationContextAsync(from, calculationOptions); var(unitPrice, itemSubtotal) = await _priceCalculationService.CalculateSubtotalAsync(calculationContext); to.UnitPrice = unitPrice.FinalPrice; to.SubTotal = itemSubtotal.FinalPrice; if (itemSubtotal.DiscountAmount > 0) { to.Discount = itemSubtotal.DiscountAmount; } } } if (item.BundleItem != null) { if (_shoppingCartSettings.ShowProductBundleImagesOnShoppingCart) { await from.MapAsync(to.Image, MediaSettings.CartThumbBundleItemPictureSize, to.ProductName); } } else { if (_shoppingCartSettings.ShowProductImagesOnShoppingCart) { await from.MapAsync(to.Image, MediaSettings.CartThumbPictureSize, to.ProductName); } } var itemWarnings = new List <string>(); var isValid = await ShoppingCartValidator.ValidateCartItemsAsync(new[] { from }, itemWarnings); if (!isValid) { to.Warnings.AddRange(itemWarnings); } var cart = await ShoppingCartService.GetCartItemsAsync(customer, shoppingCartType, _services.StoreContext.CurrentStore.Id); var attrWarnings = new List <string>(); isValid = await ShoppingCartValidator.ValidateProductAttributesAsync(item, cart, attrWarnings); if (!isValid) { to.Warnings.AddRange(attrWarnings); } }
public virtual async Task <string> FormatAttributesAsync( ProductVariantAttributeSelection selection, Product product, Customer customer = null, string separator = "<br />", bool htmlEncode = true, bool includePrices = true, bool includeProductAttributes = true, bool includeGiftCardAttributes = true, bool includeHyperlinks = true, ProductBatchContext batchContext = null) { Guard.NotNull(selection, nameof(selection)); Guard.NotNull(product, nameof(product)); customer ??= _workContext.CurrentCustomer; using var pool = StringBuilderPool.Instance.Get(out var result); if (includeProductAttributes) { var languageId = _workContext.WorkingLanguage.Id; var attributes = await _productAttributeMaterializer.MaterializeProductVariantAttributesAsync(selection); var attributesDic = attributes.ToDictionary(x => x.Id); // Key: ProductVariantAttributeValue.Id, value: calculated attribute price adjustment. var priceAdjustments = includePrices && _catalogSettings.ShowVariantCombinationPriceAdjustment ? await _priceCalculationService.CalculateAttributePriceAdjustmentsAsync(product, selection, 1, _priceCalculationService.CreateDefaultOptions(false, customer, null, batchContext)) : new Dictionary <int, CalculatedPriceAdjustment>(); foreach (var kvp in selection.AttributesMap) { if (!attributesDic.TryGetValue(kvp.Key, out var pva)) { continue; } foreach (var value in kvp.Value) { var valueStr = value.ToString().EmptyNull(); var pvaAttribute = string.Empty; if (pva.IsListTypeAttribute()) { var pvaValue = pva.ProductVariantAttributeValues.FirstOrDefault(x => x.Id == valueStr.ToInt()); if (pvaValue != null) { pvaAttribute = "{0}: {1}".FormatInvariant( pva.ProductAttribute.GetLocalized(x => x.Name, languageId), pvaValue.GetLocalized(x => x.Name, languageId)); if (includePrices) { if (_shoppingCartSettings.ShowLinkedAttributeValueQuantity && pvaValue.ValueType == ProductVariantAttributeValueType.ProductLinkage && pvaValue.Quantity > 1) { pvaAttribute = pvaAttribute + " × " + pvaValue.Quantity; } if (priceAdjustments.TryGetValue(pvaValue.Id, out var adjustment)) { if (adjustment.Price > 0) { pvaAttribute += $" (+{adjustment.Price})"; } else if (adjustment.Price < 0) { pvaAttribute += $" (-{adjustment.Price * -1})"; } } } if (htmlEncode) { pvaAttribute = pvaAttribute.HtmlEncode(); } } } else if (pva.AttributeControlType == AttributeControlType.MultilineTextbox) { string attributeName = pva.ProductAttribute.GetLocalized(x => x.Name, languageId); pvaAttribute = "{0}: {1}".FormatInvariant( htmlEncode ? attributeName.HtmlEncode() : attributeName, HtmlUtils.ConvertPlainTextToHtml(valueStr.HtmlEncode())); } else if (pva.AttributeControlType == AttributeControlType.FileUpload) { if (Guid.TryParse(valueStr, out var downloadGuid) && downloadGuid != Guid.Empty) { var download = await _db.Downloads .AsNoTracking() .Include(x => x.MediaFile) .Where(x => x.DownloadGuid == downloadGuid) .FirstOrDefaultAsync(); if (download?.MediaFile != null) { var attributeText = string.Empty; var fileName = htmlEncode ? download.MediaFile.Name.HtmlEncode() : download.MediaFile.Name; if (includeHyperlinks) { // TODO: (core) add a method for getting URL (use routing because it handles all SEO friendly URLs). var downloadLink = _webHelper.GetStoreLocation(false) + "download/getfileupload/?downloadId=" + download.DownloadGuid; attributeText = $"<a href=\"{downloadLink}\" class=\"fileuploadattribute\">{fileName}</a>"; } else { attributeText = fileName; } string attributeName = pva.ProductAttribute.GetLocalized(a => a.Name, languageId); pvaAttribute = "{0}: {1}".FormatInvariant( htmlEncode ? attributeName.HtmlEncode() : attributeName, attributeText); } } } else { // TextBox, Datepicker pvaAttribute = "{0}: {1}".FormatInvariant(pva.ProductAttribute.GetLocalized(x => x.Name, languageId), valueStr); if (htmlEncode) { pvaAttribute = pvaAttribute.HtmlEncode(); } } result.Grow(pvaAttribute, separator); } } } if (includeGiftCardAttributes && product.IsGiftCard) { var gci = selection.GiftCardInfo; if (gci != null) { // Sender. var giftCardFrom = product.GiftCardType == GiftCardType.Virtual ? (await _localizationService.GetResourceAsync("GiftCardAttribute.From.Virtual")).FormatInvariant(gci.SenderName, gci.SenderEmail) : (await _localizationService.GetResourceAsync("GiftCardAttribute.From.Physical")).FormatInvariant(gci.SenderName); // Recipient. var giftCardFor = product.GiftCardType == GiftCardType.Virtual ? (await _localizationService.GetResourceAsync("GiftCardAttribute.For.Virtual")).FormatInvariant(gci.RecipientName, gci.RecipientEmail) : (await _localizationService.GetResourceAsync("GiftCardAttribute.For.Physical")).FormatInvariant(gci.RecipientName); if (htmlEncode) { giftCardFrom = giftCardFrom.HtmlEncode(); giftCardFor = giftCardFor.HtmlEncode(); } result.Grow(giftCardFrom, separator); result.Grow(giftCardFor, separator); } } return(result.ToString()); }