How can cart error messages be cleared? If I a user adds a voucher code which he is not allowed to used (e.g. wrong shipping country) the cart gets an error:
promotion-not-eligible
.
Is there a way to acknowledge this error? Because no matter what I do the error will stay in the cart…
I assume the only way is to keep an own error state and show only new errors… as also suggested in a TODO comment in shopware frontends, but maybe there are better options? https://github.com/shopware/frontends/blob/3764736e52fffb7f7abeb4c044dee2adc812cbb6/packages/composables/src/useCart.ts#L147
Because this will be quite tricky, as an error can happen multiple times and putting it on the ignore list after initial showing up will cause it to not shop up the next time it happens again..
Another question related to this is:
Why is the cart adding success messages to the errors object?
This is an example error key which shows up after successfully adding a promotion code...
promotion-discount-added-7a26337b71e449cc860347af8b6b2931
You can add a custom cart validator. The example in the docs shows how to add errors but you may just as well remove errors you deem unnecessary.
public function validate(Cart $cart, ErrorCollection $errorCollection, SalesChannelContext $salesChannelContext): void
{
// $errorCollection contains all errors added by other validators
foreach ($errorCollection as $error) {
if ($error->getMessageKey() === 'promotion-not-found') {
$errorCollection->remove($error->getId());
}
}
// errors added previously to the cart, e.g. by processors
foreach ($cart->getErrors() as $error) {
if ($error->getMessageKey() === 'promotion-not-found') {
$cart->getErrors()->remove($error->getId());
}
}
}
Why is the cart adding success messages to the errors object?
That's indeed not ideal. Judging from this comment, this was meant to be a workaround of some sorts and shouldn't be considered best practice. The ErrorCollection
should only contain actual errors.