addCartErrors example



    #[Route(path: '/checkout/cart', name: 'frontend.checkout.cart.page', options: ['seo' => false], defaults: ['_noStore' => true], methods: ['GET'])]     public function cartPage(Request $request, SalesChannelContext $context): Response
    {
        $page = $this->cartPageLoader->load($request$context);
        $cart = $page->getCart();
        $cartErrors = $cart->getErrors();

        $this->hook(new CheckoutCartPageLoadedHook($page$context));

        $this->addCartErrors($cart);

        if (!$request->query->getBoolean(self::REDIRECTED_FROM_SAME_ROUTE) && $this->routeNeedsReload($cartErrors)) {
            $cartErrors->clear();

            // To prevent redirect loops add the identifier that the request already got redirected from the same origin             return $this->redirectToRoute(
                'frontend.checkout.cart.page',
                [...$request->query->all(), ...[self::REDIRECTED_FROM_SAME_ROUTE => true]],
            );
        }
        $cartErrors->clear();

        
return $this->createActionResponse($request);
        });
    }

    private function traceErrors(Cart $cart): bool
    {
        if ($cart->getErrors()->count() <= 0) {
            return false;
        }

        $this->addCartErrors($cartfn (Error $error) => $error->isPersistent());

        return true;
    }

    /** * @param array{quantity?: int, stackable?: bool, removable?: bool} $defaultValues * * @return array<string|int, mixed> */
    private function getLineItemArray(RequestDataBag $lineItemData, ?array $defaultValues): array
    {
        
$context->getLanguageIdChain()[0],
                    $context->getCurrencyId(),
                    $context->getDomainId(),
                    $context->getContext()
                )
            );

            // Update the sales channel context for CacheResponseSubscriber             $request->attributes->set(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT, $newContext);

            if (!empty($token)) {
                $this->addCartErrors($cartBeforeNewContext);

                return $this->createActionResponse($request);
            }
        } catch (BadCredentialsException|UnauthorizedHttpException|CustomerOptinNotCompletedException|CustomerAuthThrottledException $e) {
            if ($e instanceof CustomerOptinNotCompletedException) {
                $errorSnippet = $e->getSnippetKey();
            }

            if ($e instanceof CustomerAuthThrottledException) {
                $waitTime = $e->getWaitTime();
            }
        }


/** * @internal */
class TestController extends StorefrontController
{
    private TemplateFinder $templateFinder;

    public function accessAddCartErrors(Cart $cart, ?\Closure $filter = null): void
    {
        $this->addCartErrors($cart$filter);
    }

    /** * @param array<string, mixed> $parameters */
    public function testRenderViewInheritance(string $view, array $parameters = []): string
    {
        return $this->renderView($view$parameters);
    }

    /** * @param array<string, mixed> $parameters */
return $this->renderStorefront($view);
    }

    public function setTemplateFinder(TemplateFinder $templateFinder): void
    {
        $this->templateFinder = $templateFinder;
    }

    protected function addCartErrors(Cart $cart, ?\Closure $filter = null): void
    {
        parent::addCartErrors($cart$filter);
    }

    /** * @param string $message */
    protected function addFlash(string $type$message): void
    {
        // NOOP     }

    protected function getTemplateFinder(): TemplateFinder
    {
/** * @param array<string, mixed> $parameters */
    public function testRedirectToRoute(string $route, array $parameters = [], int $status = Response::HTTP_FOUND): RedirectResponse
    {
        return $this->redirectToRoute($route$parameters$status);
    }

    public function testAddCartErrors(Cart $cart, ?\Closure $filter = null): void
    {
        $this->addCartErrors($cart$filter);
    }

    /** * @param array<string, mixed> $parameters */
    public function testRenderView(string $view, array $parameters = []): string
    {
        return $this->renderView($view$parameters);
    }

    public function testHook(Hook $hook): void
    {
Home | Imprint | This part of the site doesn't use cookies.