I have several difficulties to write a plugin.
First, I need to get information from our own reward point database to be able to manage our reward points system.
So far, I managed to create user when a nopcustomer is registered and create a purchase ticket in our system when en order is paid.
Now, I want to be able to load information about our user when a nopcommerce customer (registered to our loyalty system) logged in.
The idea was to override WorkContext with my own LoyaltyWorkContext and used it, but I strugle on that point. So fat, I did :
public partial class LoyaltyWebWorkContext : WebWorkContext, ILoyaltyWorkContext
{
#region Fields
private LoyaltyResponse _cachedLoyaltyCustomer;
#endregion
#region ctor
public LoyaltyWebWorkContext(
CookieSettings cookieSettings,
CurrencySettings currencySettings,
IAuthenticationService authenticationService,
ICurrencyService currencyService,
ICustomerService customerService,
IGenericAttributeService genericAttributeService,
IHttpContextAccessor httpContextAccessor,
ILanguageService languageService,
IStoreContext storeContext,
IStoreMappingService storeMappingService,
IUserAgentHelper userAgentHelper,
IVendorService vendorService,
IWebHelper webHelper,
LocalizationSettings
localizationSettings,
TaxSettings taxSettings) : base(cookieSettings, currencySettings, authenticationService, currencyService, customerService, genericAttributeService, httpContextAccessor, languageService, storeContext, storeMappingService, userAgentHelper, vendorService, webHelper, localizationSettings, taxSettings)
{
}
#endregion
public override async Task<Customer> GetCurrentCustomerAsync()
{
Console.Beep();
return await base.GetCurrentCustomerAsync();
}
public virtual async Task SetCurrentLoyaltyCustomerAsync(LoyaltyResponse loyaltyCustomer = null, Customer customer = null)
{
if (loyaltyCustomer != null)
{
_cachedLoyaltyCustomer = loyaltyCustomer;
}
await base.SetCurrentCustomerAsync(customer);
}
public virtual async Task<LoyaltyResponse> GetCurrentLoyaltyCustomerAsync()
{
if (_cachedLoyaltyCustomer != null)
return _cachedLoyaltyCustomer;
await base.GetCurrentCustomerAsync();
return _cachedLoyaltyCustomer;
}
}
}
In NopStartup, I add :
services.AddScoped<ILoyaltyWorkContext, LoyaltyWebWorkContext>();
services.AddScoped<WebWorkContext, LoyaltyWebWorkContext>();
Thinking WebWorkContext will be overrident and take LoyaltyWebWorkContext in place, as I thought I understood from this post :
https://www.nopcommerce.com/en/boards/topic/94588/override-controller-action-problem-with-prameters
But I think I misunderstood, because it doesn't work.
So is it possible to achieve something like that?