@@ -243,104 +243,13 @@ protected virtual decimal GetPreselectedPrice(Product product, PriceCalculationC
243243 return result ;
244244 }
245245
246- //protected virtual decimal GetPreselectedPriceOld(Product product, ProductBundleItemData bundleItem, IList<ProductBundleItemData> bundleItems)
247- //{
248- // var taxRate = decimal.Zero;
249- // var attributesTotalPriceBase = decimal.Zero;
250- // var preSelectedPriceAdjustmentBase = decimal.Zero;
251- // var isBundle = (product.ProductType == ProductType.BundledProduct);
252- // var isBundleItemPricing = (bundleItem != null && bundleItem.Item.BundleProduct.BundlePerItemPricing);
253- // var isBundlePricing = (bundleItem != null && !bundleItem.Item.BundleProduct.BundlePerItemPricing);
254- // var bundleItemId = (bundleItem == null ? 0 : bundleItem.Item.Id);
255- // var attributes = (isBundle ? new List<ProductVariantAttribute>() : _productAttributeService.GetProductVariantAttributesByProductId(product.Id));
256- // var selectedAttributes = new NameValueCollection();
257- // List<ProductVariantAttributeValue> selectedAttributeValues = null;
258-
259- // foreach (var attribute in attributes)
260- // {
261- // int preSelectedValueId = 0;
262- // ProductVariantAttributeValue defaultValue = null;
263-
264- // if (attribute.ShouldHaveValues())
265- // {
266- // var pvaValues = _productAttributeService.GetProductVariantAttributeValues(attribute.Id);
267- // if (pvaValues.Count == 0)
268- // continue;
269-
270- // foreach (var pvaValue in pvaValues)
271- // {
272- // ProductBundleItemAttributeFilter attributeFilter = null;
273-
274- // if (bundleItem.FilterOut(pvaValue, out attributeFilter))
275- // continue;
276-
277- // if (preSelectedValueId == 0 && attributeFilter != null && attributeFilter.IsPreSelected)
278- // preSelectedValueId = attributeFilter.AttributeValueId;
279-
280- // if (!isBundlePricing && pvaValue.IsPreSelected)
281- // {
282- // decimal attributeValuePriceAdjustment = GetProductVariantAttributeValuePriceAdjustment(pvaValue);
283- // decimal priceAdjustmentBase = _taxService.GetProductPrice(product, attributeValuePriceAdjustment, out taxRate);
284-
285- // preSelectedPriceAdjustmentBase = decimal.Add(preSelectedPriceAdjustmentBase, priceAdjustmentBase);
286- // }
287- // }
288-
289- // // value pre-selected by a bundle item filter discards the default pre-selection
290- // if (preSelectedValueId != 0 && (defaultValue = pvaValues.FirstOrDefault(x => x.Id == preSelectedValueId)) != null)
291- // defaultValue.IsPreSelected = true;
292-
293- // if (defaultValue == null)
294- // defaultValue = pvaValues.FirstOrDefault(x => x.IsPreSelected);
295-
296- // if (defaultValue == null && attribute.IsRequired)
297- // defaultValue = pvaValues.First();
298-
299- // if (defaultValue != null)
300- // selectedAttributes.AddProductAttribute(attribute.ProductAttributeId, attribute.Id, defaultValue.Id, product.Id, bundleItemId);
301- // }
302- // }
303-
304- // if (!isBundle && selectedAttributes.Count > 0)
305- // {
306- // string attributeXml = selectedAttributes.CreateSelectedAttributesXml(product.Id, attributes, _productAttributeParser, _services.Localization,
307- // _downloadService, _catalogSettings, _httpRequestBase, new List<string>(), true, bundleItemId);
308-
309- // selectedAttributeValues = _productAttributeParser.ParseProductVariantAttributeValues(attributeXml).ToList();
310-
311- // var combinations = _productAttributeService.GetAllProductVariantAttributeCombinations(product.Id);
312-
313- // var selectedCombination = combinations.FirstOrDefault(x => _productAttributeParser.AreProductAttributesEqual(x.AttributesXml, attributeXml));
314-
315- // if (selectedCombination != null && selectedCombination.IsActive)
316- // product.MergeWithCombination(selectedCombination);
317- // }
318-
319- // if (_catalogSettings.EnableDynamicPriceUpdate && !isBundlePricing)
320- // {
321- // if (selectedAttributeValues != null)
322- // {
323- // selectedAttributeValues.Each(x => attributesTotalPriceBase += GetProductVariantAttributeValuePriceAdjustment(x));
324- // }
325- // else
326- // {
327- // attributesTotalPriceBase = preSelectedPriceAdjustmentBase;
328- // }
329- // }
330-
331- // if (bundleItem != null)
332- // {
333- // bundleItem.AdditionalCharge = attributesTotalPriceBase;
334- // }
335-
336- // var result = GetFinalPrice(product, bundleItems, _services.WorkContext.CurrentCustomer, attributesTotalPriceBase, true, 1, bundleItem);
337- // return result;
338- //}
339-
340246 public virtual PriceCalculationContext CreatePriceCalculationContext ( IEnumerable < int > productIds = null )
341247 {
342248 var context = new PriceCalculationContext ( ) ;
343249
250+ if ( _catalogSettings . PriceDisplayType == PriceDisplayType . Hide || _catalogSettings . PriceDisplayType == PriceDisplayType . PriceWithoutDiscountsAndAttributes )
251+ return context ;
252+
344253 context . Attributes = new LazyMultimap < ProductVariantAttribute > ( x =>
345254 {
346255 "load {0}" . FormatInvariant ( string . Join ( ";" , x ) ) . Dump ( ) ;
@@ -349,11 +258,14 @@ public virtual PriceCalculationContext CreatePriceCalculationContext(IEnumerable
349258 return result ;
350259 } , productIds ) ;
351260
352- context . AttributeCombinations = new LazyMultimap < ProductVariantAttributeCombination > ( x =>
261+ if ( _catalogSettings . PriceDisplayType == PriceDisplayType . PreSelectedPrice )
353262 {
354- var result = _productAttributeService . GetProductVariantAttributeCombinations ( x ) ;
355- return result ;
356- } , productIds ) ;
263+ context . AttributeCombinations = new LazyMultimap < ProductVariantAttributeCombination > ( x =>
264+ {
265+ var result = _productAttributeService . GetProductVariantAttributeCombinations ( x ) ;
266+ return result ;
267+ } , productIds ) ;
268+ }
357269
358270 return context ;
359271 }
@@ -527,48 +439,27 @@ public virtual decimal GetFinalPrice(
527439 /// Get the lowest possible price for a product.
528440 /// </summary>
529441 /// <param name="product">Product</param>
442+ /// <param name="context">Object with cargo data for better performance</param>
530443 /// <param name="displayFromMessage">Whether to display the from message.</param>
531444 /// <returns>The lowest price.</returns>
532- public virtual decimal GetLowestPrice ( Product product , out bool displayFromMessage )
445+ public virtual decimal GetLowestPrice ( Product product , PriceCalculationContext context , out bool displayFromMessage )
533446 {
534447 if ( product == null )
535448 throw new ArgumentNullException ( "product" ) ;
536449
537450 if ( product . ProductType == ProductType . GroupedProduct )
538451 throw Error . InvalidOperation ( "Choose the other override for products of type grouped product." ) ;
539452
540- displayFromMessage = false ;
453+ if ( context == null )
454+ context = CreatePriceCalculationContext ( null ) ;
541455
542- IList < ProductBundleItemData > bundleItems = null ;
543456 bool isBundlePerItemPricing = ( product . ProductType == ProductType . BundledProduct && product . BundlePerItemPricing ) ;
544457
545- if ( isBundlePerItemPricing )
546- {
547- bundleItems = _productService . GetBundleItems ( product . Id ) ;
458+ displayFromMessage = isBundlePerItemPricing ;
548459
549- // sepcial case: one bundle item with one attribute and one attribute value and one attribute combination
550- if ( bundleItems . Count == 1 )
551- {
552- var firstBundleItem = bundleItems . First ( ) ;
553- if ( firstBundleItem . Item . Product . ProductVariantAttributes . Count == 1 )
554- {
555- var firstAttribute = firstBundleItem . Item . Product . ProductVariantAttributes . First ( ) ;
556- if ( firstAttribute . ProductVariantAttributeValues . Count == 1 )
557- {
558- var firstAttributeValue = firstAttribute . ProductVariantAttributeValues . First ( ) ;
559- firstBundleItem . AdditionalCharge = firstAttributeValue . PriceAdjustment ;
560-
561- var combinations = _productAttributeService . GetAllProductVariantAttributeCombinations ( firstBundleItem . Item . ProductId ) ;
562- if ( combinations . Count == 1 )
563- {
564- firstBundleItem . Item . Product . MergeWithCombination ( combinations . First ( ) ) ;
565- }
566- }
567- }
568- }
569- }
460+ // TODO(?): attribute price adjustments were not regarded here cause of performance
570461
571- decimal lowestPrice = GetFinalPrice ( product , bundleItems , _services . WorkContext . CurrentCustomer , decimal . Zero , true , int . MaxValue ) ;
462+ decimal lowestPrice = GetFinalPrice ( product , null , _services . WorkContext . CurrentCustomer , decimal . Zero , true , int . MaxValue ) ;
572463
573464 if ( product . LowestAttributeCombinationPrice . HasValue && product . LowestAttributeCombinationPrice . Value < lowestPrice )
574465 {
@@ -581,6 +472,24 @@ public virtual decimal GetLowestPrice(Product product, out bool displayFromMessa
581472 lowestPrice = product . LowestAttributeCombinationPrice ?? decimal . Zero ;
582473 }
583474
475+ if ( ! displayFromMessage && product . ProductType != ProductType . BundledProduct )
476+ {
477+ var attributes = context . Attributes . Ensure ( product . Id ) ;
478+ displayFromMessage = attributes . Any ( x => x . ProductVariantAttributeValues . Any ( y => y . PriceAdjustment != decimal . Zero ) ) ;
479+ }
480+
481+ if ( ! displayFromMessage && product . HasTierPrices && ! isBundlePerItemPricing )
482+ {
483+ var tierPrices = product . TierPrices
484+ . OrderBy ( tp => tp . Quantity )
485+ . FilterByStore ( _services . StoreContext . CurrentStore . Id )
486+ . FilterForCustomer ( _services . WorkContext . CurrentCustomer )
487+ . ToList ( )
488+ . RemoveDuplicatedQuantities ( ) ;
489+
490+ displayFromMessage = ( tierPrices . Count > 0 && ! ( tierPrices . Count == 1 && tierPrices . First ( ) . Quantity <= 1 ) ) ;
491+ }
492+
584493 return lowestPrice ;
585494 }
586495
@@ -602,7 +511,7 @@ public virtual decimal GetLowestPrice(Product product, out bool displayFromMessa
602511 if ( product . ProductType != ProductType . GroupedProduct )
603512 throw Error . InvalidOperation ( "Choose the other override for products not of type grouped product." ) ;
604513
605- lowestPriceProduct = product ;
514+ lowestPriceProduct = null ;
606515 decimal ? lowestPrice = null ;
607516
608517 foreach ( var associatedProduct in associatedProducts )
@@ -620,6 +529,10 @@ public virtual decimal GetLowestPrice(Product product, out bool displayFromMessa
620529 lowestPriceProduct = associatedProduct ;
621530 }
622531 }
532+
533+ if ( lowestPriceProduct == null )
534+ lowestPriceProduct = associatedProducts . FirstOrDefault ( ) ;
535+
623536 return lowestPrice ;
624537 }
625538
@@ -660,23 +573,6 @@ public virtual decimal GetPreselectedPrice(Product product, PriceCalculationCont
660573 result = GetPreselectedPrice ( product , context , null , null ) ;
661574 }
662575
663- //if (product.ProductType == ProductType.BundledProduct)
664- //{
665- // var bundleItems = _productService.GetBundleItems(product.Id);
666-
667- // foreach (var bundleItem in bundleItems.Where(x => x.Item.Product.CanBeBundleItem()))
668- // {
669- // // fetch bundleItems.AdditionalCharge for all bundle items
670- // var unused = GetPreselectedPriceOld(bundleItem.Item.Product, bundleItem, bundleItems);
671- // }
672-
673- // result = GetPreselectedPriceOld(product, null, bundleItems);
674- //}
675- //else
676- //{
677- // result = GetPreselectedPriceOld(product, null, null);
678- //}
679-
680576 return result ;
681577 }
682578
0 commit comments