Order Completed Event?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
Hi,

Is there a way to hook up with some kind of OrderCompletedEvent?
I need to inform a third party whenever and Order is completed by calling a WebService at their end.

Thanks!
13 years ago
Hi,

No, at this moment there no events. But you can add a trigger on update for orders table.

For example, in your trigger check is the order was completed, then mark somewhere in another table to notify your web service. Also, add a new task which will reads this table and notifies your web service. Or something like that.
13 years ago
OK, thanks!
13 years ago
Personally if you don't mind changing the nopCommerce source I would just add the event to the order object (saves having to hit the database unecessarily).

Adding an event is fairly trivial. Just add the following code to your order object.


        public static event OrderCompletedEventHandler OrderCompleted;
        protected internal void OnOrderCompleted(EventArgs e) {
            if (OrderCompleted != null)
                OrderCompleted(this, new EventArgs());
        }

        public delegate void OrderCompletedEventHandler(object sender, EventArgs e);


The event is static so that we don't have to create a handler on a specific instance.

Then you need to trigger the event. Just find the point in code (in OrderManager) when the order is "completed" and call OnOrderCompleted:


    public static class OrderService
    {
        public static void ProcessOrder(Order order) {
            // process order the raise event

            order.OnOrderCompleted(EventArgs.Empty);
        }
    }
}


(note that these are just demo service and method names)

Now you've done that you can put your web service code anywhere (such as your own assembly). It just needs to have the signature defined by the delegate:


        static void Order_OrderCompleted(object sender, EventArgs e)
        {
            // call web service here
            Console.Write("Order has been completed");
            Console.ReadLine();
        }


Of course we need to register our handler. The easiest place is in global.asax Application_Start:


Order.OrderCompleted += new Order.OrderCompletedEventHandler(YourAssembly.OrderExtensions.Order_OrderCompleted);


If you are looking to hook up to many events  like this then I prefer to keep everything separate. Check out this article (towards the bottom) where I am using StructureMap to execute StartUp tasks when the application starts (a great way of registering your handlers)
13 years ago
ah great, thanks!
13 years ago
Is something like this on the roadmap anywhere? I'm looking for something exactly like this, too. Ideally we could supply our own plugin module that can hook into events like orderplaced, ordercompleted, etc... and get all of that info in the eventargs. I need to call into some WCF services to create software licenses, etc... so it would be nice to be able to just write a module with a particular interface and hook into that. Don't want to recompile the source everytime an update is out.
13 years ago
Yes there's a new EventContext class. It's quite basic in 1.7; I can't tell you yet if it's been expanded in 1.8.

My post asking for more info has gone unanswered but I've had success tapping into an order placed as follows:

In global.asax Application_BeginRequest:
EventContext.Current.OrderPlaced += FleetObserverLiteNopCommercePlugin.OrderPlacedHandler;

Static class in new library project:

    public static class FleetObserverLiteNopCommercePlugin
    {

        /// <summary>
        /// OrderPlaced event handler
        /// </summary>
        /// <remarks>Needs to be threadsafe</remarks>
        public static void OrderPlacedHandler(object sender, OrderEventArgs e)
        {
.... DO STUFF

HTH and if anyone has better ideas for how to use it please post.
13 years ago
You're rebuilding the source to do that though, correct? I think this should be able to be accomplished as a true plugin.
13 years ago
What I posted is what you asked for. My code is a plugin DLL that is totally seperate. The only change to NopCommerce I had to make is attaching my handler in global.asax.

Whether you'd have to recompile each time there's an upgrade depends on whether the Nop guys break the interfaces or not.
13 years ago
kingboyk wrote:
What I posted is what you asked for. My code is a plugin DLL that is totally seperate. The only change to NopCommerce I had to make is attaching my handler in global.asax.

Whether you'd have to recompile each time there's an upgrade depends on whether the Nop guys break the interfaces or not.


Hmm. I assumed that since global.asax was able to reference your assembly you had to compile nopCommerce with your reference, but perhaps thats not the case (i'm not an asp.net guy, more winforms)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.