How to Add a Quote Backend Validation Rule

As we have seen from our experience, another common task for a developer is to perform a quote backend validation before an order is placed. Magento have several quote backend validations by default. Let's see how we can add our own.

We can find several quote validation rules defined by default from Magento and especially in the Magento Quote module. They are defined in the di.xml file of the module as object arguments for the class Magento\Quote\Model\ValidationRules\QuoteValidationComposite. Every class is implemented the QuoteValidationRuleInterface. That means that all validation rule classes must have a validate(Quote $quote) function.

So in our custom's module di.xml file, we define our quote validation rule class as argument. In our CustomValidationRule class we add a simple quote validation with an error message just for needs of our blog post. You can simple adjust the validate function with your validation you want to perform.

The full example as extension can be found here.

Feel free to share this post and ask your questions in the comments below.

Till next time!