删除错误集合中已添加的错误。购物车错误未消除 - shopware6

Remove error already added in error collection. The cart error isn't removed - shopware6

提问人:Amir Dora. 提问时间:11/16/2023 最后编辑:MarcusAmir Dora. 更新时间:11/18/2023 访问量:27

问:

如何解决这个问题?

我将错误添加到错误集合中,但每次完成验证时,我认为都会重新初始化,因此它是 null。之前添加的错误不会显示在 errorCollection-error 中,也不会显示?在树枝中,我仍然可以看到错误。$errorCollection$cart->errors

class MinimumOrderValueValidator implements CartValidatorInterface
{
    private $config;

    public function __construct(SystemConfigService $config)
    {
        $this->config = $config;
    }

    public function validate(Cart $cart, ErrorCollection $errorCollection, SalesChannelContext $context): void
    {
        // Check if there is at least one physical product in the cart
        $hasPhysicalProduct = $this->hasPhysicalProduct($cart);
        $errorId = 'minimum_order_value_error'; // Identifier for the error

        if (!$hasPhysicalProduct) {
            // No physical products found, so no need to check for the minimum order value
            $this->removeMinimumOrderValidationError($cart, $errorId);

            return;
        }

        $minimumOrderValue = $this->config->get('MyAppTheme.config.MinimumOrderValue') ?? 10; // Default to 10 if not defined

        // Check if the total cart value is less than the minimum order value
        if ($cart->getPrice()->getTotalPrice() < floatval($minimumOrderValue)) {
            // Check if an error with the specific ID already exists
            $existingError = $errorCollection->get($errorId);

            if (!$existingError) {
                // Add error as the total cart value is less than the minimum order value
                $errorCollection->add(new MinimumOrderValueNotReachedException(
                    $errorId,
                    strval($minimumOrderValue)
                ));
            }
        } else {
            // Cart value meets the minimum order value, remove the error if it exists
            $this->removeMinimumOrderValidationError($cart, $errorId);
        }
    }

    // Function to check if the cart contains at least one physical product
    private function hasPhysicalProduct(Cart $cart): bool
    {
        foreach ($cart->getLineItems() as $item) {
            $states = $item->getStates();
            if (!empty($states) && 'is-physical' === $states[0]) {
                return true;
            }
        }

        return false;
    }

    // Function to remove MinimumOrderValueNotReachedException from the error collection
    private function removeMinimumOrderValidationError(Cart $cart, $errorId): void
    {
        foreach ($cart->getErrors() as $error) {
            if ('promotion-not-found' === $error->getMessageKey()) {
                $cart->getErrors()->remove($errorId);
            }
        }
    }
}
装 店 装6

评论


答:

1赞 Amir Dora. 11/16/2023 #1

好的,我发现错误添加到 ,即使它们显示在树枝中但没有添加到 。我将错误添加到购物车并能够从那里删除它们,并且没有重新初始化。errorCollection$cart

$cart->addErrors(new MinimumOrderValueNotReachedException(
    $errorId,
    strval($minimumOrderValue)
));