Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #896 Add a cart rule for affiliates
Added a button to remove the assignment of a customer to an affiliate on customer edit page.
- Loading branch information
Showing
10 changed files
with
153 additions
and
39 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/Smartstore.Core/Checkout/Affiliates/Rules/AffiliateRuleOptionsProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Smartstore.Core.Data; | ||
using Smartstore.Core.Rules; | ||
using Smartstore.Core.Rules.Rendering; | ||
|
||
namespace Smartstore.Core.Checkout.Affiliates.Rules | ||
{ | ||
public partial class AffiliateRuleOptionsProvider : IRuleOptionsProvider | ||
{ | ||
private readonly SmartDbContext _db; | ||
|
||
public AffiliateRuleOptionsProvider(SmartDbContext db) | ||
{ | ||
_db = db; | ||
} | ||
|
||
public int Order => 0; | ||
|
||
public bool Matches(string dataSource) | ||
{ | ||
return dataSource == KnownRuleOptionDataSourceNames.Affiliate; | ||
} | ||
|
||
public async Task<RuleOptionsResult> GetOptionsAsync(RuleOptionsContext context) | ||
{ | ||
if (context.DataSource != KnownRuleOptionDataSourceNames.Affiliate) | ||
{ | ||
return null; | ||
} | ||
|
||
var result = new RuleOptionsResult(); | ||
|
||
var pager = _db.Affiliates | ||
.AsNoTracking() | ||
.Include(x => x.Address) | ||
.Where(x => x.Active) | ||
.ToFastPager(); | ||
|
||
while ((await pager.ReadNextPageAsync<Affiliate>()).Out(out var affiliates)) | ||
{ | ||
result.AddOptions(context, affiliates.Select(x => new RuleValueSelectListOption | ||
{ | ||
Value = x.Id.ToString(), | ||
Text = x.Address?.GetFullName()?.NullEmpty() ?? StringExtensions.NotAvailable, | ||
Hint = x.Address?.Email | ||
})); | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/Smartstore.Core/Checkout/Rules/Impl/CustomerAffiliateRule.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Smartstore.Core.Data; | ||
using Smartstore.Core.Rules; | ||
|
||
namespace Smartstore.Core.Checkout.Rules.Impl | ||
{ | ||
internal class CustomerAffiliateRule : IRule | ||
{ | ||
private readonly SmartDbContext _db; | ||
|
||
public CustomerAffiliateRule(SmartDbContext db) | ||
{ | ||
_db = db; | ||
} | ||
|
||
public async Task<bool> MatchAsync(CartRuleContext context, RuleExpression expression) | ||
{ | ||
var customer = context.Customer; | ||
if (customer != null && !customer.IsSystemAccount && customer.AffiliateId != 0 && expression.HasListMatch(customer.AffiliateId)) | ||
{ | ||
var isValidAffiliate = await _db.Affiliates.AnyAsync(x => x.Id == customer.AffiliateId && !x.Deleted && x.Active); | ||
return isValidAffiliate; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.