allBlogsList

How to Automate Commerce Sync

Sitecore Commerce can be setup to trigger to automatically synchronize commerce. In the below example a Sitecore task will be setup. The task will trigger the synchronize of commerce data into Sitecore.

  1. Create an item named Commerce Sync using the Command template at /sitecore/system/Tasks/Commands/Commerce Sync
  2. Set the type field to "XCommerce.Task.CommerceSync, XCommerce". The reference class will be created at a later step.
  3. Set the method to "Run."
  4. Create an item name Commerce Sync using the Schedule template at /sitecore/system/Tasks/Schedules/Commerce Sync
  5. Set the Command to the Commerce template created in Step 1.
  6. Set the Schedule field to "20140101|99990101|127|0.02:00" this will have the sync job run every 2 hours.
  7. Create a class CommerceSync.
using Sitecore.Commerce.Products;
using Sitecore.Commerce.Services.Products;
using Sitecore.Diagnostics;
using System;

namespace XCommerce.Task
{
    public class CommerceSync
    {
    }
}

The class will have a function named run. The function will be executed every time the job is triggered. The first step in the function is to save a message to the log file. This will allow notification that the job was successfully started in the log file.

        public void Run()
        {
            Log.Info("Run Commerce Sync", this);
        }

The CommerceSynchronization will use the ProductSynchronizationProvider.SynchronizeProductsRequest. This request specifies the language for the product data being synchronized. In the below example the language has been hardcoded to en. This would need to be dynamic for multilanguage websites.

                SynchronizeProductsRequest request = new SynchronizeProductsRequest {
                    Language = "en",
                    Direction = Direction.Both
                };

To help make resilient code a try catch is added around the code. Additional logging is added to help with debugging if any errors are encountered.

            Log.Info("Run Commerce Sync", this);
            try
            {
                ProductSynchronizationProvider provider = new ProductSynchronizationProvider();
                
                SynchronizeProductsRequest request = new SynchronizeProductsRequest {
                    Language = "en",
                    Direction = Direction.Both
                };

                provider.SynchronizeProducts(request);
            }
            catch(Exception e)
            {
                Log.Fatal("Exception in CommerceSync:", e, this);
            }
            Log.Info("End Commerce Sync", this);

Below is the complete class.

using Sitecore.Commerce.Products;
using Sitecore.Commerce.Services.Products;
using Sitecore.Diagnostics;
using System;

namespace XCommerce.Task
{
    public class CommerceSync
    {
        public void Run()
        {
            Log.Info("Run Commerce Sync", this);
            try
            {
                ProductSynchronizationProvider provider = new ProductSynchronizationProvider();
                
                SynchronizeProductsRequest request = new SynchronizeProductsRequest {
                    Language = "en",
                    Direction = Direction.Both
                };

                provider.SynchronizeProducts(request);
            }
            catch(Exception e)
            {
                Log.Fatal("Exception in CommerceSync:", e, this);
            }
            Log.Info("End Commerce Sync", this);
        }
    }
}

Once the above code is compiled, the commerce connect will automatically run with the Sitecore jobs. Out of box Sitecore job scheduler kicks off the job every 10 minutes. You are able to change the frequency of the job check in App_Config\Include\Sitecore.Processing.config where name equals Master_Database_Agent. The below xml config set the job to run every minute.

      <agent type="Sitecore.Tasks.DatabaseAgent" method="Run" interval="00:01:00" name="Master_Database_Agent">
        <param desc="database">master</param>
        <param desc="schedule root">/sitecore/system/tasks/schedules</param>
        <LogActivity>true</LogActivity>
      </agent>

With the above approach you are able to setup Sitecore Commerce to sync data using a schedule batch.