Skip to content

Commit ee9a7c0

Browse files
committed
Minor perf improvement
1 parent 3b58ca0 commit ee9a7c0

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/Libraries/SmartStore.Services/Discounts/DiscountService.cs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using SmartStore.Services.Customers;
1313
using SmartStore.Services.Common;
1414
using SmartStore.Services.Configuration;
15+
using SmartStore.Services.Orders;
1516

1617
namespace SmartStore.Services.Discounts
1718
{
@@ -317,52 +318,55 @@ public virtual bool IsDiscountValid(Discount discount, Customer customer, string
317318
}
318319

319320
//check date range
320-
DateTime now = DateTime.UtcNow;
321-
int storeId = _storeContext.CurrentStore.Id;
321+
var now = DateTime.UtcNow;
322+
var store = _storeContext.CurrentStore;
322323

323324
if (discount.StartDateUtc.HasValue)
324325
{
325-
DateTime startDate = DateTime.SpecifyKind(discount.StartDateUtc.Value, DateTimeKind.Utc);
326+
var startDate = DateTime.SpecifyKind(discount.StartDateUtc.Value, DateTimeKind.Utc);
326327
if (startDate.CompareTo(now) > 0)
327328
return false;
328329
}
329330
if (discount.EndDateUtc.HasValue)
330331
{
331-
DateTime endDate = DateTime.SpecifyKind(discount.EndDateUtc.Value, DateTimeKind.Utc);
332+
var endDate = DateTime.SpecifyKind(discount.EndDateUtc.Value, DateTimeKind.Utc);
332333
if (endDate.CompareTo(now) < 0)
333334
return false;
334335
}
335336

336337
if (!CheckDiscountLimitations(discount, customer))
337338
return false;
338339

339-
// discount requirements
340-
var requirements = discount.DiscountRequirements;
340+
// better not to apply discounts if there are gift cards in the cart cause the customer could "earn" money through that.
341+
if (discount.DiscountType == DiscountType.AssignedToOrderTotal || discount.DiscountType == DiscountType.AssignedToOrderSubTotal)
342+
{
343+
var cart = customer.ShoppingCartItems
344+
.Filter(ShoppingCartType.ShoppingCart, store.Id)
345+
.ToList();
346+
347+
if (cart.Any(x => x.Product.IsGiftCard))
348+
return false;
349+
}
350+
351+
// discount requirements
352+
var requirements = discount.DiscountRequirements;
341353
foreach (var req in requirements)
342354
{
343-
var requirementRule = LoadDiscountRequirementRuleBySystemName(req.DiscountRequirementRuleSystemName, storeId);
355+
var requirementRule = LoadDiscountRequirementRuleBySystemName(req.DiscountRequirementRuleSystemName, store.Id);
344356
if (requirementRule == null)
345357
continue;
346358

347-
var request = new CheckDiscountRequirementRequest()
359+
var request = new CheckDiscountRequirementRequest
348360
{
349361
DiscountRequirement = req,
350362
Customer = customer,
351-
Store = _storeContext.CurrentStore
363+
Store = store
352364
};
365+
353366
if (!requirementRule.Value.CheckRequirement(request))
354367
return false;
355368
}
356369

357-
// better not to apply discounts if there are gift cards in the cart cause the customer could "earn" money through that.
358-
if (discount.DiscountType == DiscountType.AssignedToOrderTotal || discount.DiscountType == DiscountType.AssignedToOrderSubTotal)
359-
{
360-
var cart = customer.GetCartItems(ShoppingCartType.ShoppingCart, storeId);
361-
362-
if (cart.Any(x => x.Item.Product.IsGiftCard))
363-
return false;
364-
}
365-
366370
return true;
367371
}
368372

0 commit comments

Comments
 (0)