Can you provide hooks (global event handlers)?

9 years ago
Hi developers,

I think it would be really useful if you could provide hooks or a global event handler scheme to allow us developers further down the chain to extend nopCommerce *without having to touch the original source code*. I'm talking here about important events such as a user account being created, an order being made, or an order status changing.

My favourite idea for doing this is to create a DLL in the NopCommerce solution with a class containing empty functions for each "event". You would control the signatures (probably by creating an abstract class in the main codebase), but users can put their own code inside. Obviously you'd pass an EventArgs-like object along which would contain all relevant variables. Better still would be if we could feed our own subclassed objects into nopCommerce by allowing us a bit-part in object creation.

An alternative option is to use the plugin model. I've experience with this (albeit not in a web scenario) in another open source project which I'm a developer of (AutoWikiBrowser). Basically you declare an interface that plugins must support and then search the bin or a subfolder for instances of that plugin using reflection at runtime. In this scenario you'd probably only want to allow one plugin to be loaded I think.

This would be really simple to knock up and would, unless I'm missing something, make the product a lot more extendible.

I'm willing to write or help write this but you know a lot lot better than me where the hooks should go, not to mention that you're better programmers than me, so you could probably knock it up in no time.
9 years ago
Andrei has kindly opened a work item for this at http://nopcommerce.codeplex.com/WorkItem/View.aspx?WorkItemId=8401&ProjectName=nopCommerce  Vote for it if you'd like this feature.
8 years ago
Hi,

Has this feature been implemented? I need to intercept the EventContext.OnOrderUpdated event. If the order is complete, I need to execute a certain action. Without recompiling the source, is there a way to override a class?
8 years ago
Yes, it was implemented. The file you need to look at is EventContext.cs in the Nop.BusinessLogic library.

To run code when an order is paid, for example, I added the following code to Application_BeginRequest() in global.asax:
EventContext.Current.OrderPaid += FleetObserverLiteNopCommercePlugin.OrderPaidHandler;


In my plugin I declared the handler as follows:
public static void OrderPaidHandler(object sender, OrderEventArgs e)
{
...
}


It's as simple as that!
6 years ago
Hi, I was wondering if this is still the way to go if you need to hook in to specific events such as order created from a custom plugin?

I couldn't find the EventContext class in nop v 2.80 source?

Thanks!
6 years ago
Yes, a class in your plugin needs to implement the IConsumer<EventName> interface.

An example in the source would be Nop.Plugin.SMS.Verizon.OrderPlacedEventConsumer
6 years ago
great, thanks andy!