Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon Dingley 1474 posts 3451 karma points c-trib
    Nov 10, 2016 @ 10:15
    Simon Dingley
    0

    CreateShipment Event doesn't appear to be triggered

    We have added an event handler to trigger an email notification when an order status is set to "Shipped".

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        ShipmentService.StatusChanged += ShipmentServiceStatusChanged;
        ShipmentService.Creating += ShipmentServiceOnCreating;
    }
    
    private void ShipmentServiceStatusChanged(IShipmentService sender, Merchello.Core.Events.StatusChangeEventArgs<IShipment> e)
    {
        var validKeys = new[] { Merchello.Core.Constants.DefaultKeys.ShipmentStatus.Shipped };
    
        foreach (var shipment in e.StatusChangedEntities)
        {
            if (!validKeys.Contains(shipment.ShipmentStatus.Key)) continue;
    
            LogHelper.Info<UmbracoEventHandler>(string.Format("Raising notification trigger for shippment no. {0}", shipment.ShipmentNumber));
    
            Notification.Trigger("OrderShipped", shipment, new [] { shipment.Email });
        }
    }
    
    private void ShipmentServiceOnCreating(IShipmentService sender, Merchello.Core.Events.NewEventArgs<IShipment> newEventArgs)
    {
        if (newEventArgs.Entity.ShipmentStatus.Key == Merchello.Core.Constants.DefaultKeys.ShipmentStatus.Shipped)
        {
            ShipmentServiceStatusChanged(sender, new StatusChangeEventArgs<IShipment>(newEventArgs.Entity));
        }
    }
    

    This is for a small company and so the workflow means that the store owners will fulfil the order setting the Shipment status to Shipped when they create the shipment. Because the status is set when the Shipment is created the ShipmentService.StatusChanged event is not fired and I get why that is. So, in order to get around this, I subscribed to the ShipmentService.Creating event and check the status there and if it says "Shipped" will trigger the ShipmentService.StatusChanged event. Unfortunately the ShipmentService.Creating doesn't seem to fire.

    Am I missing something or does anyone have a different approach?

    Thanks, Simon

  • Simon Dingley 1474 posts 3451 karma points c-trib
    Nov 10, 2016 @ 11:31
    Simon Dingley
    100

    I remembered that I had a custom task in the OrderPreparationShipmentCreate taskchain and so I've amended it so that it triggers the notification as follows:

    Notification.Trigger("OrderShipped", value, new[] { value.Email });
    

    This achieves what I set out to do but would be interested to know why I couldn't do it via the ShipmentService.Creating event handler.

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Nov 10, 2016 @ 15:02
    Rusty Swayne
    1

    Hey Simon,

    I need to spend some time looking at the Created and Creating events on some services - so this does not surprise me. I've found there are inconsistencies between services depending on whether on how things are initially persisted, meaning if the entity is created and then saved or one of the create with key methods are used without a save.

  • Simon Dingley 1474 posts 3451 karma points c-trib
    Nov 10, 2016 @ 16:04
    Simon Dingley
    1

    At least I got it to work by other means and to be honest I'm not sure of the advantages or disadvantages of doing this at the moment via an Event handler or using the task chain. The task chain is probably more in keeping with the Merchello way of doing things :)

  • Lesley 107 posts 350 karma points
    Nov 23, 2016 @ 22:44
    Lesley
    0

    Hi Rusty,

    I am having the same issue as Simon in that I need to be able to trigger the OrderShipped email when shipment is created if the shipment status is Shipped.

    We don't have a custom task in the OrderPreparationShipmentCreate task change that I can amend.

    Are you able to provide some sample code on how to create a custom task to trigger the notification?

  • Simon Dingley 1474 posts 3451 karma points c-trib
    Nov 24, 2016 @ 10:15
    Simon Dingley
    0

    Hi Lesley,

    See the Merchello source for the RemoveShipmentOrderItemsFromInventoryAndPersistShipmentTask:

    https://github.com/Merchello/Merchello/blob/merchello-dev/src/Merchello.Core/Chains/ShipmentCreation/RemoveShipmentOrderItemsFromInventoryAndPersistShipmentTask.cs

    This was pretty much what I adapted for my own purposes and then see the following extract from my class where I trigger the email:

    // If we are creating a shipment with a status of Shipped from the outset we want to trigger the OrderShipped notification
    if (value.ShipmentStatus.Key == Merchello.Core.Constants.DefaultKeys.ShipmentStatus.Shipped)
    {
        LogHelper.Info<UmbracoEventHandler>(string.Format("Raising notification trigger for shippment no. {0}", value.ShipmentNumber));
        Notification.Trigger("OrderShipped", value, new[] { value.Email });
    }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies