DEV Community

Cover image for 
Dynamics 365 Commerce POS: Add validation before adding an item to cart
Marij Siddiqui
Marij Siddiqui

Posted on

Dynamics 365 Commerce POS: Add validation before adding an item to cart

Problem:
The Customer requires validation to be added before an item is added into 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 into 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
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Deployment:
Build your CRT extension and place it in the Retail Server’s extension folder. Drop extension .dll in 'RetailServer\WebRoot\bin\Ext' folder and modify CommerceRuntime.Ext configuration file by adding extension assembly reference in composition section of 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 365\70\Retail Modern POS\ClientBroker\ext' folder and add reference of extension assembly in 'CommerceRuntime.MPOSOffline.ext' config file. Attach it to the IIS process of the Retail Server.

For Retail SDK build, you’d need to add this CRT extension to 'CommerceRuntime.ext' config in 'RetailSDK>Assets' and 'customization.settings' in 'RetailSDK>BuildTools'.

Top comments (0)