Code Optimization

Interesting things about software development and code optimization

NopCommerce customization - Events

Hello friends,


this post will be about NopCommerce Events and how to react on them. I will show how to react to the Order Placed Event.

For that we need to add our own class and inherit IConsumer<OrderPlacedEvent> so NopCommerce would automatically use it, 

here is the code:

namespace Nop.Plugin.MyProductFeeder
{
    public class MyOrderPlacedEvent : IConsumer<OrderPlacedEvent>
    {
        private readonly IPluginFinder _pluginFinder;
        private readonly IOrderService _orderService;
        private readonly IStoreContext _storeContext;

        private readonly Nop.Services.Common.IGenericAttributeService _genericAttributeService;

        public MyOrderPlacedEvent(
IPluginFinder pluginFinder, IOrderService orderService, IStoreContext storeContext, Nop.Services.Common.IGenericAttributeService genericAttributeService) { this._pluginFinder = pluginFinder; this._orderService = orderService; this._storeContext = storeContext; this._genericAttributeService = genericAttributeService; } /// <summary> /// Handles the event. /// </summary> /// <param name="eventMessage">The event message.</param> public void HandleEvent(OrderPlacedEvent eventMessage) { eventMessage.Order.OrderStatus = OrderStatus.Pending; eventMessage.Order.OrderNotes.Add(new OrderNote() { CreatedOnUtc = DateTime.UtcNow, DisplayToCustomer = true, Note = "Please, confirm your order by email!" }); _orderService.UpdateOrder(eventMessage.Order); } } }

So my main idea was to force user to confirm order by email with a unique link in it.

Not too complex but may not be so obvious for someone.


Thank you and see you there: NopCommerce customization - Full cycle of product adding in batch



1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y



Pingbacks and trackbacks (1)+

Loading