Loading...
Area: Optimizely B2B Commerce

Handlers and helpers associated with custom address fields

Recommended reading 

Available Handlers and Helpers

The data for the configurable address fields are populated within the BillTo and ShipTo handler chains. The following CustomerHelper methods are actually responsible for aggregating, formatting, and returning validating information for use by the BillTo and ShipTo address forms on the Storefront.

CustomerValidationDto PopulateBillToValidation(IUnitOfWork unitOfWork, GetCustomerSettingsResult settings, Customer billTo);
CustomerValidationDto PopulateShipToValidation(IUnitOfWork unitOfWork, GetCustomerSettingsResult settings, Customer billTo, Customer shipTo, bool isNew = false);

These methods are currently used in the BillTo and ShipTo handler chains. An example of their use can be seen below.

var userAndBillToResult = this.CustomerHelper.GetUserAndBillTo(
    unitOfWork, // This should be injected in the constructor.
    result, // This is returned by the handler chain.
    ... // The rest of the arguments should come from a parameter to the handler.
);
var customerSettings = this.HandlerFactory.GetHandler<IHandler<GetSettingsParameter, GetCustomerSettingsResult>>().Execute(
    unitOfWork,
    new GetSettingsParameter(),
    new GetCustomerSettingsResult());
  
this.CustomerHelper.PopulateBillToValidation(
    unitOfWork,
    customerSettings,
    userAndBillToResult.BillTo);

Use a Custom Handler to Access the Validation Data

If you want to get access to the generated validation data, an easy way is to inject a custom handler into the chain of responsibility. The following handlers populate validation data for use by either the BillTo or ShipTo address forms.

Handler Order Comment
GetBillToHandler 500 Populates validation for the BillTo address form.
GetShipToHandler 500 Populates validation for the ShipTo address form.

Below is an example custom handler that can be used to get access to the generated validation data:

[DependencyName("ModifyBillToValidationHandler ")]
public class ModifyBillToValidationHandler : HandlerBase<GetBillToParameter, GetBillToResult>
{
    public override int Order => 501; // The order number must be higher to come after the GetBillToHandler in the handler chain.
 
    public override GetBillToResult Execute(IUnitOfWork unitOfWork, GetBillToParameter parameter, GetBillToResult result)
    {
        // You can modify the validation data here.
        // result.Validation now holds the validation data populated by the GetBillToHandler
    }
}
Do you find this information helpful? Please log in to provide feedback.

Last updated: Dec 11, 2020

Recommended reading