Problem:
The Customer requires validation to be added before an item is added to a cart in POS. For example, an item must belong to a certain list to be purchased from POS. Or any criteria whatsoever.
Development:
When an item is being added to a cart, one of the requests triggered is SaveCartVersionedDataRequest. We can add a pre-trigger to this request as a Commerce Runtime (CRT) extension. We’d need to implement the IRequestTriggerAsync interface and implement mandatory methods.
public class AddItemToCartValidationTrigger : IRequestTriggerAsync { public IEnumerable SupportedRequestTypes => new[] { typeof(SaveCartVersionedDataRequest) }; public async Task OnExecuted(Request request, Response response) { await Task.CompletedTask; } public async Task OnExecuting(Request request) { boolean criteria = true; if (criteria) // if some criteria is met, add an item to the cart { // do something } else // reject item to be added to cart { // throw commerce exception } } }
Deployment:
Build your CRT extension and place it in the Retail Server’s extension folder. Drop extension .dll in the RetailServerWebRootbinExt folder and modify CommerceRuntime.Ext the configuration file by adding an extension assembly reference in the composition section of the config file. Restart the Retail Server after adding an extension to it.
To debug locally you can also add this extension .dll in C:Program Files (x86)Microsoft Dynamics 36570Retail Modern POSClientBrokerext folder and add a reference of extension assembly in CommerceRuntime.MPOSOffline.ext config file. Attach it to the IIS process of the Retail Server.
For the Retail SDK build, you’d need to add this CRT extension to CommerceRuntime.ext config in RetailSDK>Assets and customization.settings in RetailSDK>BuildTools.