IConsumer events firing multiple times

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
I just created my first plugin to implement some business logic every time an order goes into Paid status. The plugin is loaded in the admin site and installed but the event is firing exactly 7 times no matter what I do. Is there something I'm doing wrong with plugin registration or is it normal behavior for events to fire this many times?

namespace Nop.Plugin.MyProject.EventHooks
{
    public class EventHooksPlugin : BasePlugin, IMiscPlugin
    {
        public void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
        {
            actionName = null;
            controllerName = null;
            routeValues = null;
        }
    }
}


namespace Nop.Plugin.MyProject.EventHooks
{
    public class SubscriptionEventConsumer : IConsumer<EntityUpdated<Order>> //, IMiscPlugin
    {
        private const string _pluginName = "MyProject.EventHooks";
        private readonly IPluginFinder _pluginFinder;
        private readonly IPlanService _planService;

        public SubscriptionEventConsumer(IPluginFinder pluginFinder, IPlanService planService)
        {
            this._pluginFinder = pluginFinder;
            this._planService = planService;
        }

        public void HandleEvent(EntityUpdated<Order> eventMessage)
        {
            //is plugin installed?
            var pluginDescriptor = _pluginFinder.GetPluginDescriptorBySystemName(_pluginName);
            if (pluginDescriptor == null || !pluginDescriptor.Installed)
                return;

            if (eventMessage.Entity.PaymentStatus == Core.Domain.Payments.PaymentStatus.Paid)
            {
                // insert a row into the planinstance table
                _planService.InsertPlanInstance(new PlanInstance() { OrderId = eventMessage.Entity.Id, CustomerId = eventMessage.Entity.Customer.Id });
            }
        }
    }
}
12 years ago
Normal.  If you look at OrderService.cs, method UpdateOrder - note it publishes the event.  Right click method name and "Find all References", you will see 70+.    Even during the normal order process, it gets called multiple times as parts of the order info are updated from page to page (shipping, payment, address selection, etc.)

I suggest you move this line to the top of your method

if (eventMessage.Entity.PaymentStatus == Core.Domain.Payments.PaymentStatus.Paid)
12 years ago
Makes sense. I was so preoccupied getting the plugin to work I didn't realize I was consuming the wrong event. I switched it to OrderPaidEvent and it fires once as expected.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.