Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Quan Mai
Aug 22, 2019
  2072
(3 votes)

Audit order activities

Recently I got a question regarding auditing order activities. For Catalog, you got information of who edited/deleted catalog items at when, in the Application Log (may you guess, in Commerce manager!)

However if you make changes to orders, there is no log recorded. While that functionalities are not available out of box, you can add it to your site quite easily, fortunately. The key is to listen to order events and record a log for it.

    [ModuleDependency(typeof(CommerceInitialization))]
    public class OrderEventInitialization : IConfigurableModule
    {
        public void ConfigureContainer(ServiceConfigurationContext context)
        {
        }

        private void Current_OrderGroupDeleted(object sender, OrderEventArgs e)
        {
            LogManager.WriteLog("Order deleted", PrincipalInfo.Current.Name, e.OrderLink.ToString(), "Commerce Manager", "SYSTEM", "This is an example note", true);
        }

        private void Current_OrderGroupUpdated(object sender, OrderEventArgs e)
        {
            LogManager.WriteLog("Order updated", PrincipalInfo.Current.Name, e.OrderLink.ToString(), "Commerce Manager", "SYSTEM", "This is an example note", true);
        }

        public void Initialize(InitializationEngine context)
        {
            var orderEvents = context.Locate.Advanced.GetInstance<IOrderEvents>();
            orderEvents.SavedOrder += Current_OrderGroupUpdated;
            orderEvents.DeletedOrder += Current_OrderGroupDeleted;
        }

        public void Uninitialize(InitializationEngine context)
        { }
    }

And the result. Note that I used "SYSTEM" so the log will be recorded to System log. You might want to use "order" instead, the log records will be in Application Log like the catalog log.

This has a few notes:

  • IOrderEvents are local only. I.e. if the orders are updated/deleted from another site, they will not be triggered. If you are using Commerce Manager and CSR UI, then you will need to do the same with CSR UI site
  • If you are using an older version without IOrderEvents, and can't upgrade at the moment, you can try to use OrderContext.Current.OrderGroupUpdated and OrderContext.Current.OrderGroupDeleted which are roughly equivalent with slightly different signatures. Note that these are not fired for changes outside of the "legacy" order types, so if you are using Serializable cart, they will not be fired.
  • You only know that an order was updated, but not which was updated (items added/removed, payments added/removed...). The Saved event happens after the order was saved, so it can be tricky finding out what has changed. Technically you can listen to SavingOrder, then look for things that were changed.
  • If you go with above approach, note that this will block the thread saving order until you are done. If you do some heavy processing, it'd make sense to process in an async manner. 
Aug 22, 2019

Comments

Praful Jangid
Praful Jangid Aug 23, 2019 06:37 AM

Nice post, Thanks. It's useful information for me. :)

Please login to comment.
Latest blogs
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog