11using System ;
2+ using System . Collections . Generic ;
23using System . Linq ;
4+ using Newtonsoft . Json ;
35using SmartStore . Core . Domain . Orders ;
6+ using SmartStore . Core . Logging ;
47using SmartStore . Core . Plugins ;
5- using SmartStore . Services . Discounts ;
8+ using SmartStore . DiscountRules . Settings ;
9+ using SmartStore . Services . Catalog ;
610using SmartStore . Services . Customers ;
11+ using SmartStore . Services . Discounts ;
712using SmartStore . Services . Orders ;
8- using SmartStore . Services . Catalog ;
913using SmartStore . Services . Tax ;
10- using SmartStore . Core . Localization ;
11- using SmartStore . DiscountRules . Settings ;
12- using Newtonsoft . Json ;
1314
1415namespace SmartStore . DiscountRules
1516{
@@ -21,15 +22,18 @@ public partial class HadSpentAmountRule : DiscountRequirementRuleBase
2122 private readonly IOrderService _orderService ;
2223 private readonly IPriceCalculationService _priceCalculationService ;
2324 private readonly ITaxService _taxService ;
25+ private readonly ILogger _logger ;
2426
2527 public HadSpentAmountRule (
2628 IOrderService orderService ,
2729 IPriceCalculationService priceCalculationService ,
28- ITaxService taxService )
30+ ITaxService taxService ,
31+ ILogger logger )
2932 {
30- this . _orderService = orderService ;
31- this . _priceCalculationService = priceCalculationService ;
32- this . _taxService = taxService ;
33+ _orderService = orderService ;
34+ _priceCalculationService = priceCalculationService ;
35+ _taxService = taxService ;
36+ _logger = logger ;
3337 }
3438
3539 public override bool CheckRequirement ( CheckDiscountRequirementRequest request )
@@ -88,14 +92,34 @@ private bool CheckTotalHistoryRequirement(CheckDiscountRequirementRequest reques
8892
8993 private bool CheckCurrentSubTotalRequirement ( CheckDiscountRequirementRequest request )
9094 {
91- var cartItems = request . Customer . GetCartItems ( ShoppingCartType . ShoppingCart , request . Store . Id ) ;
95+ var spentAmount = decimal . Zero ;
9296
93- decimal spentAmount = decimal . Zero ;
94- decimal taxRate = decimal . Zero ;
95- foreach ( var sci in cartItems )
97+ try
9698 {
97- // includeDiscounts == true produces a stack overflow!
98- spentAmount += sci . Item . Quantity * _taxService . GetProductPrice ( sci . Item . Product , _priceCalculationService . GetUnitPrice ( sci , false ) , out taxRate ) ;
99+ var taxRate = decimal . Zero ;
100+ var cartItems = request . Customer . GetCartItems ( ShoppingCartType . ShoppingCart , request . Store . Id ) ;
101+
102+ foreach ( var cartItem in cartItems )
103+ {
104+ var product = cartItem . Item . Product ;
105+ Dictionary < string , object > mergedValuesClone = null ;
106+
107+ // we must reapply merged values because CheckCurrentSubTotalRequirement uses price calculation and is called by it itself.
108+ // this can cause wrong discount calculation if the cart contains a product several times.
109+ if ( product . MergedDataValues != null )
110+ mergedValuesClone = new Dictionary < string , object > ( product . MergedDataValues ) ;
111+
112+ // includeDiscounts == true produces a stack overflow!
113+ spentAmount += cartItem . Item . Quantity * _taxService . GetProductPrice ( product , _priceCalculationService . GetUnitPrice ( cartItem , false ) , out taxRate ) ;
114+
115+ if ( mergedValuesClone != null )
116+ product . MergedDataValues = new Dictionary < string , object > ( mergedValuesClone ) ;
117+ }
118+ }
119+ catch ( Exception exception )
120+ {
121+ _logger . Error ( exception ) ;
122+ return false ;
99123 }
100124
101125 return spentAmount >= request . DiscountRequirement . SpentAmount ;
0 commit comments