Sitecore commerce Translate Shipping Party to Engine Entity

Site core commerce engine connect out of the box check out flow contains fields like Name, city and country, State, address and zip code. An example of the checkout process delivery flow can be viewed at https://experiencecommerce.habitathomedemo.com/checkout/delivery once some item is added to cart.

In this blog, I am going to explain how to extend the shipping properties so it can accommodate more shipping properties like organization and FaxNumber.

The shipping properties will be stored in site core commerce in serialize Model to Sitecore.Commerce.Core.Party Model which is consumed by commerce engine. Default translations of the Commerce Party will translate the properties like First name, LastName, Email, Address1.To add information like Organization, we need to extend the commerce default pipeline.

Behind the scenes SItecore commerce performs the transformation of CommerceParty Model to Party Model in Sitecore.Commerce.Engine.Connect.Pipelines.Customers. TranslateEntityToParty in Sitecore.Commerce.Engine.Connect class library.

So we need to extend the TranslateEntityToParty class and register our entity in the site core pipeline.

To do that lets follow the steps below

Create class with name TranslateEntityToParty and inherit it with TranslateEntityToODataModel

Inheriting TranslateEntityToODataModel will let you overwrite Translate method that takes 2 arguments Commerce Party Model (site core Model) and Party Model (commerce Engine model).

Default site core commerce connect implementation of TranslateEntityToParty will have the following properties  AddressName, FirstName, LastName, Email, Address1, Address2, City, State, Country, ZipPostalCode and phone number.

Let’s add the following code to add information like organization

protected override void Translate(
      CommerceParty source,
      Party destination,
      TranslateEntityToPartyResult result)
    {
      base.Translate(source, destination, result);
      destination.ExternalId = source.PartyId;
      destination.AddressName = source.Name;
      destination.FirstName = source.FirstName;
      destination.LastName = source.LastName;
      destination.Email = source.Email;
      destination.Address1 = source.Address1;
      destination.Address2 = source.Address2;
      destination.City = source.City;
      destination.State = source.RegionName ?? source.State;
      destination.StateCode = source.RegionCode;
      destination.Country = source.Country;
      destination.CountryCode = source.CountryCode;
      destination.ZipPostalCode = source.ZipPostalCode;
      destination.PhoneNumber = source.PhoneNumber;
      destination.IsPrimary = source.IsPrimary;
      destination.Organization = source.Company;
    }

Register the pipeline with site core with the following configuration

 

<translate.entityToParty>
        <processor patch:instead="processor[@type='Sitecore.Commerce.Engine.Connect.Pipelines.Customers.TranslateEntityToParty, Sitecore.Commerce.Engine.Connect']"
               type="HI.Commerce.Engine.Connect.Pipelines.Customers.TranslateEntityToParty, HI.Commerce.Feature.Cart">
        </processor>
      </translate.entityToParty>

Full code and configuration of custom pipeline is available at https://github.com/ramakrishnaila/sitecore-commerce/blob/master/TranslateEntityToParty.cs

And https://github.com/ramakrishnaila/sitecore-commerce/blob/master/z.HI.Commerce.Connect.Shipping.config

Site core engine connect will translate the shipping data from site core to commerce engine by using pipeline. Site core gives the flexibility to extend the information transferred from site core to commerce via commerce connect pipelines.