<plugin root>/src/Core/Checkout/Cart/Address. Since the validator in the following example will be called
CustomCartValidator, its directory will be
Shopware\Core\Checkout\Cart\CartValidatorInterface. This forces you to also implement a
CartValidatorInterfaceand therefore implement a
validatemethod. This method has access to some important parts of the checkout, such as the cart and the current sales channel context. Also you have access to the error collection, which may or may not contain errors from other earlier validators.
customPayload, and it expects a value in there.
Shopware\Core\Checkout\Cart\Error\Error. Most likely you want to create your own error class here, which will be done in the next step.
returnstatement afterwards. If you wouldn't return here, it would add an error to the error collection for each invalid line item, resulting in several errors displayed on the checkout or the cart page. E.g. if you had four invalid items in your cart, four separate errors would be shown. This way, only one message is shown, so it depends on what you're validating and what you want to happen.
CustomCartBlockedErrorand should be located in a
Errordirectory in the same domain as the validator. Since the validator was located in the directory
<plugin root>/src/Core/Checkout/Cart/Custom, the error class will be located in the directory
Shopware\Core\Checkout\Cart\Error\Error, which asks you to implement a few methods:
getId: Here you have to return a unique ID, since your error will be saved via this ID in the error collection. In this example,
getMessageKey: The snippet key of the message to be displayed. In this example it will be
custom-line-item-blocked, which is important
getLevel: The kind of error, available are
error. Depending on that decision, the error will be printed in a blue,
blockOrder: Return a boolean on whether this exception should block the possibility to actually finish the checkout.
true, hence the error level defined earlier. It wouldn't make sense to block the checkout, but only display a notice.
getParameters: You can add custom payload here. Technically any plugin or code could read the errors of the cart and act accordingly.
custom-line-item-blockedin your custom error class
CustomCartBlockedError. Once your validator finds an invalid line item in your cart, Shopware is going to search for a respective snippet. In the cart, Shopware will be looking for the following snippet key:
checkout.custom-line-item-blocked. Meanwhile it will be looking for a key
error.custom-line-item-blockedin the checkout steps. This way you could technically define two different messages for the cart and the following checkout steps.