public virtual PriceCalculationOptions CreateDefaultOptions(
            bool forListing,
            Customer customer                = null,
            Currency targetCurrency          = null,
            ProductBatchContext batchContext = null)
        {
            customer ??= batchContext?.Customer ?? _workContext.CurrentCustomer;

            var store        = batchContext?.Store ?? _storeContext.CurrentStore;
            var language     = _workContext.WorkingLanguage;
            var priceDisplay = _catalogSettings.PriceDisplayType;
            var taxInclusive = _workContext.GetTaxDisplayTypeFor(customer, store.Id) == TaxDisplayType.IncludingTax;
            var determinePreselectedPrice = forListing && priceDisplay == PriceDisplayType.PreSelectedPrice;

            targetCurrency ??= _workContext.WorkingCurrency;
            batchContext ??= _productService.CreateProductBatchContext(null, store, customer, false);

            var options = new PriceCalculationOptions(batchContext, customer, store, language, targetCurrency)
            {
                IsGrossPrice = _taxSettings.PricesIncludeTax,
                TaxInclusive = taxInclusive,
                IgnorePercentageDiscountOnTierPrices = !_catalogSettings.ApplyPercentageDiscountOnTierPrice,
                IgnorePercentageTierPricesOnAttributePriceAdjustments = !_catalogSettings.ApplyTierPricePercentageToAttributePriceAdjustments,
                IgnoreDiscounts            = forListing && priceDisplay == PriceDisplayType.PriceWithoutDiscountsAndAttributes,
                DetermineLowestPrice       = forListing && priceDisplay == PriceDisplayType.LowestPrice,
                DeterminePreselectedPrice  = determinePreselectedPrice,
                ApplyPreselectedAttributes = determinePreselectedPrice,
                TaxFormat        = _currencyService.GetTaxFormat(null, taxInclusive, PricingTarget.Product, language),
                PriceRangeFormat = T("Products.PriceRangeFrom").Value,
                RoundingCurrency = targetCurrency == _primaryCurrency ? _workContext.WorkingCurrency : targetCurrency
            };

            return(options);
        }
예제 #2
0
 public PriceCalculationOptions(ProductBatchContext batchContext, Customer customer, Store store, Language language, Currency targetCurrency)
 {
     BatchContext   = batchContext;
     Customer       = customer;
     Store          = store;
     Language       = language;
     TargetCurrency = targetCurrency;
     CashRounding   = new();
 }
        /// <summary>
        /// Creates a new options instance.
        /// </summary>
        /// <param name="batchContext">The product batch context to use. Required.</param>
        /// <param name="customer">The current customer. Required.</param>
        /// <param name="store">The current store. Required.</param>
        /// <param name="language">The working language. Required.</param>
        /// <param name="targetCurrency">The target currency for money exchange. Required.</param>
        public PriceCalculationOptions(ProductBatchContext batchContext, Customer customer, Store store, Language language, Currency targetCurrency)
        {
            Guard.NotNull(batchContext, nameof(batchContext));
            Guard.NotNull(customer, nameof(customer));
            Guard.NotNull(store, nameof(store));
            Guard.NotNull(language, nameof(language));
            Guard.NotNull(targetCurrency, nameof(targetCurrency));

            BatchContext   = batchContext;
            Customer       = customer;
            Store          = store;
            Language       = language;
            TargetCurrency = targetCurrency;
            CashRounding   = new();
        }
예제 #4
0
        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());
        }
예제 #5
0
        private static async Task <IEnumerable <TierPrice> > LoadTierPrices(Product product, Customer customer, ProductBatchContext batchContext)
        {
            if (!product.HasTierPrices)
            {
                return(Enumerable.Empty <TierPrice>());
            }

            if (!batchContext.TierPrices.FullyLoaded)
            {
                await batchContext.TierPrices.LoadAllAsync();
            }

            var tierPrices = await batchContext.TierPrices.GetOrLoadAsync(product.Id);

            return(tierPrices.RemoveDuplicatedQuantities());
        }
예제 #6
0
        protected virtual async Task <decimal?> GetMinimumTierPriceAsync(Product product, Customer customer, int quantity, ProductBatchContext batchContext)
        {
            if (!product.HasTierPrices)
            {
                return(decimal.Zero);
            }

            var     previousQty = 1;
            decimal?result      = null;
            var     tierPrices  = await LoadTierPrices(product, customer, batchContext);

            foreach (var tierPrice in tierPrices)
            {
                if (quantity < tierPrice.Quantity || tierPrice.Quantity < previousQty)
                {
                    continue;
                }

                if (tierPrice.CalculationMethod == TierPriceCalculationMethod.Fixed)
                {
                    result = tierPrice.Price;
                }
                else if (tierPrice.CalculationMethod == TierPriceCalculationMethod.Percental)
                {
                    result = product.Price - (product.Price / 100m * tierPrice.Price);
                }
                else
                {
                    result = product.Price - tierPrice.Price;
                }

                previousQty = tierPrice.Quantity;
            }

            return(result);
        }
예제 #7
0
        public virtual async Task <ShoppingCartSubtotal> GetShoppingCartSubtotalAsync(IList <OrganizedShoppingCartItem> cart, bool?includeTax = null, ProductBatchContext batchContext = null)
        {
            includeTax ??= _workContext.TaxDisplayType == TaxDisplayType.IncludingTax;

            var result = await GetCartSubtotalAsync(cart, includeTax.Value, batchContext);

            return(new ShoppingCartSubtotal
            {
                SubtotalWithoutDiscount = new(result.SubtotalWithoutDiscount, _primaryCurrency),
                SubtotalWithDiscount = new(result.SubtotalWithDiscount, _primaryCurrency),
                DiscountAmount = new(result.DiscountAmount, _primaryCurrency),
                AppliedDiscount = result.AppliedDiscount,
                TaxRates = result.TaxRates,
                LineItems = result.LineItems
            });