From e31500c841f667b94453fa80a07e82aac9f21a4b Mon Sep 17 00:00:00 2001 From: support Date: Thu, 26 Nov 2026 21:13:33 +0100 Subject: [PATCH] Let a campaign choose customers by whether they have orders The "customer has orders" condition of a campaign filtered on a helper property that the query never filled, so counting or sending such a campaign failed with an unsupported expression, and the condition alone was ignored, as the conditions on customers were only read when another one was set. It now reads the customer's last purchase date, which every placed order sets, and counts as a customer condition of its own. --- .../Services/Campaigns/CampaignService.cs | 7 +++--- .../Campaigns/CampaignServiceTests.cs | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Business/Grand.Business.Marketing/Services/Campaigns/CampaignService.cs b/src/Business/Grand.Business.Marketing/Services/Campaigns/CampaignService.cs index 1fb226b79..c62a994dd 100644 --- a/src/Business/Grand.Business.Marketing/Services/Campaigns/CampaignService.cs +++ b/src/Business/Grand.Business.Marketing/Services/Campaigns/CampaignService.cs @@ -124,6 +124,7 @@ public virtual async Task> CustomerSubscripti IPagedList model; if (campaign.CustomerCreatedDateFrom.HasValue || campaign.CustomerCreatedDateTo.HasValue || campaign.CustomerHasShoppingCart is not (CampaignCondition.All and CampaignCondition.All) || + campaign.CustomerHasOrders != CampaignCondition.All || campaign.CustomerLastActivityDateFrom.HasValue || campaign.CustomerLastActivityDateTo.HasValue || campaign.CustomerLastPurchaseDateFrom.HasValue || campaign.CustomerLastPurchaseDateTo.HasValue || campaign.CustomerTags.Count > 0 || campaign.CustomerGroups.Count > 0) @@ -176,14 +177,15 @@ from customers in joined break; } + //every order placed sets the customer's last purchase date switch (campaign.CustomerHasOrders) { //customer has order case CampaignCondition.True: - query = query.Where(x => x.IsHasOrders); + query = query.Where(x => x.LastPurchaseDateUtc != null); break; case CampaignCondition.False: - query = query.Where(x => !x.IsHasOrders); + query = query.Where(x => x.LastPurchaseDateUtc == null); break; } @@ -326,7 +328,6 @@ private class CampaignCustomerHelp public DateTime LastActivityDateUtc { get; set; } public DateTime? LastPurchaseDateUtc { get; set; } public bool HasShoppingCartItems { get; set; } - public bool IsHasOrders { get; } public ICollection CustomerTags { get; set; } public ICollection NewsletterCategories { get; set; } public ICollection CustomerGroups { get; set; } = new List(); diff --git a/src/Tests/Grand.Business.Marketing.Tests/Services/Campaigns/CampaignServiceTests.cs b/src/Tests/Grand.Business.Marketing.Tests/Services/Campaigns/CampaignServiceTests.cs index db67a6abb..e4f5c7408 100644 --- a/src/Tests/Grand.Business.Marketing.Tests/Services/Campaigns/CampaignServiceTests.cs +++ b/src/Tests/Grand.Business.Marketing.Tests/Services/Campaigns/CampaignServiceTests.cs @@ -170,6 +170,28 @@ await _newsLetterSubscriptionRepository.InsertAsync(new NewsLetterSubscription Assert.HasCount(1, result); } + [TestMethod] + [DataRow(CampaignCondition.True, "buyer")] + [DataRow(CampaignCondition.False, "browser")] + public async Task CustomerSubscriptions_CustomerHasOrders_KeepsTheMatchingCustomers(CampaignCondition hasOrders, string expected) + { + await _customerRepository.InsertAsync(new Customer { Id = "buyer", LastPurchaseDateUtc = DateTime.UtcNow.AddDays(-1) }); + await _customerRepository.InsertAsync(new Customer { Id = "browser" }); + await _newsLetterSubscriptionRepository.InsertAsync(new NewsLetterSubscription { StoreId = "1", Active = true, CustomerId = "buyer", Email = "buyer@x.com" }); + await _newsLetterSubscriptionRepository.InsertAsync(new NewsLetterSubscription { StoreId = "1", Active = true, CustomerId = "browser", Email = "browser@x.com" }); + + //alone, and next to another customer condition + foreach (var hasCart in new[] { CampaignCondition.All, CampaignCondition.False }) + { + var campaign = new Campaign { StoreId = "1", CustomerHasOrders = hasOrders, CustomerHasShoppingCart = hasCart }; + + var result = await _campaignService.CustomerSubscriptions(campaign); + + Assert.HasCount(1, result); + Assert.AreEqual(expected, result[0].CustomerId); + } + } + [TestMethod] public async Task SendCampaignTest() {