getShippingMethods example

$this->footerLoader->load($request$context)
            );

            $criteria = new Criteria();
            $criteria->setTitle('generic-page::shipping-methods');

            $event = new ShippingMethodRouteRequestEvent($requestnew Request()$context$criteria);
            $this->eventDispatcher->dispatch($event);

            $shippingMethods = $this->shippingMethodRoute
                ->load($event->getStoreApiRequest()$context$event->getCriteria())
                ->getShippingMethods();

            $page->setSalesChannelShippingMethods($shippingMethods);

            $criteria = new Criteria();
            $criteria->setTitle('generic-page::payment-methods');

            $event = new PaymentMethodRouteRequestEvent($requestnew Request()$context$criteria);
            $this->eventDispatcher->dispatch($event);

            $paymentMethods = $this->paymentMethodRoute
                ->load($event->getStoreApiRequest()$context$event->getCriteria())
                
$this->ruleRepository->create($this->rule, $defaultContext);

        $criteria = new Criteria([$this->ruleId]);
        $criteria->addAssociation('shippingMethods');

        $searchResult = $this->ruleRepository->search($criteria$defaultContext);

        static::assertSame($this->ruleId, $searchResult->first()->getId());
        static::assertSame(
            $this->rule[0]['shippingMethods'][0]['id'],
            $searchResult->first()->getShippingMethods()->first()->getId()
        );
    }

    public function testIfShippingMethodCanBeAddedToRule(): void
    {
        $defaultContext = Context::createDefaultContext();
        $this->ruleRepository->create($this->rule, $defaultContext);

        $additionalShippingMethod = [
            'id' => Uuid::randomHex(),
            'type' => 1,
            
$this->createMock(AddressValidationFactory::class),
            $this->createMock(DataValidator::class)
        );

        $page = $checkoutConfirmPageLoader->load(
            new Request(),
            $this->getContextWithDummyCustomer()
        );

        static::assertInstanceOf(CheckoutConfirmPage::class$page);
        static::assertSame($paymentMethods$page->getPaymentMethods());
        static::assertSame($shippingMethods$page->getShippingMethods());
    }

    public function testCustomerNotLoggedInException(): void
    {
        $checkoutConfirmPageLoader = new CheckoutConfirmPageLoader(
            $this->createMock(EventDispatcher::class),
            $this->createMock(StorefrontCartFacade::class),
            $this->createMock(ShippingMethodRoute::class),
            $this->createMock(PaymentMethodRoute::class),
            $this->createMock(GenericPageLoader::class),
            $this->createMock(AddressValidationFactory::class),
            
$page = CheckoutCartPage::createFrom($page);

        if ($page->getMetaInformation()) {
            $page->getMetaInformation()->setRobots('noindex,follow');
        }

        $page->setCountries($this->getCountries($salesChannelContext));

        $page->setPaymentMethods($this->getPaymentMethods($salesChannelContext));

        $page->setShippingMethods($this->getShippingMethods($salesChannelContext));

        $page->setCart($this->cartService->get($salesChannelContext->getToken()$salesChannelContext));

        $this->eventDispatcher->dispatch(
            new CheckoutCartPageLoadedEvent($page$salesChannelContext$request)
        );

        return $page;
    }

    private function getPaymentMethods(SalesChannelContext $context): PaymentMethodCollection
    {
$this->getContainer()->get('event_dispatcher'),
            $this->getContainer()->get(StorefrontCartFacade::class),
            $this->getContainer()->get(PaymentMethodRoute::class),
            $route,
            $this->getContainer()->get(CountryRoute::class)
        );

        $context = $this->createSalesChannelContextWithNavigation();

        $result = $loader->load(new Request()$context);

        static::assertTrue($result->getShippingMethods()->has($context->getShippingMethod()->getId()));
    }

    /** * @return CheckoutCartPageLoader */
    protected function getPageLoader()
    {
        return $this->getContainer()->get(CheckoutCartPageLoader::class);
    }
}
$this->createMock(StorefrontCartFacade::class),
            $this->createMock(GenericPageLoader::class),
            $shippingMethodRoute,
        );

        $page = $offcanvasCartPageLoader->load(
            new Request(),
            $this->createMock(SalesChannelContext::class)
        );

        static::assertInstanceOf(OffcanvasCartPage::class$page);
        static::assertSame($shippingMethods$page->getShippingMethods());
    }

    public function testValidationEventIsDispatched(): void
    {
        $eventDispatcher = $this->createMock(EventDispatcher::class);
        $eventDispatcher
            ->expects(static::once())
            ->method('dispatch')
            ->with(static::isInstanceOf(OffcanvasCartPageLoadedEvent::class));

        $offcanvasCartPageLoader = new OffcanvasCartPageLoader(
            
$ids
        );
    }

    public function testSorting(): void
    {
        $shippingMethodRoute = $this->getContainer()->get(ShippingMethodRoute::class);

        $request = new Request();

        $unselectedPaymentResult = $shippingMethodRoute->load($request$this->salesChannelContext, new Criteria());
        $lastPaymentMethodId = $unselectedPaymentResult->getShippingMethods()->last()?->getId() ?? '';

        $this->salesChannelContext->getShippingMethod()->setId($lastPaymentMethodId);
        $selectedPaymentMethodResult = $shippingMethodRoute->load($request$this->salesChannelContext, new Criteria());

        static::assertInstanceOf(SortedShippingMethodRoute::class$shippingMethodRoute);
        static::assertSame($lastPaymentMethodId$selectedPaymentMethodResult->getShippingMethods()->first()?->getId());

        $traces = $this->getContainer()->get(ScriptTraces::class)->getTraces();
        static::assertArrayHasKey(ShippingMethodRouteHook::HOOK_NAME, $traces);
    }

    

        $page = $this->genericLoader->load($request$salesChannelContext);

        $page = OffcanvasCartPage::createFrom($page);

        if ($page->getMetaInformation()) {
            $page->getMetaInformation()->assign(['robots' => 'noindex,follow']);
        }

        $page->setCart($this->cartService->get($salesChannelContext->getToken()$salesChannelContext));

        $page->setShippingMethods($this->getShippingMethods($salesChannelContext));

        $this->eventDispatcher->dispatch(
            new OffcanvasCartPageLoadedEvent($page$salesChannelContext$request)
        );

        return $page;
    }

    private function getShippingMethods(SalesChannelContext $context): ShippingMethodCollection
    {
        $request = new Request();
        
$context = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation();

        /** @var CheckoutConfirmPageLoadedEvent $event */
        $event = null;
        $this->catchEvent(CheckoutConfirmPageLoadedEvent::class$event);

        $page = $this->getPageLoader()->load($request$context);

        static::assertInstanceOf(CheckoutConfirmPage::class$page);
        static::assertSame(0.0, $page->getCart()->getPrice()->getNetPrice());
        static::assertSame($context->getToken()$page->getCart()->getToken());
        static::assertCount(StorefrontPageTestConstants::AVAILABLE_SHIPPING_METHOD_COUNT, $page->getShippingMethods());
        static::assertCount(StorefrontPageTestConstants::AVAILABLE_PAYMENT_METHOD_COUNT, $page->getPaymentMethods());
        static::assertNotEmpty($page->getPaymentMethods());
        self::assertPageEvent(CheckoutConfirmPageLoadedEvent::class$event$context$request$page);
    }

    public function testItIgnoresUnavailableShippingMethods(): void
    {
        $request = new Request();
        $context = $this->createSalesChannelContextWithLoggedInCustomerAndWithNavigation();

        /** @var EntityRepository $shippingMethodRepository */
        

    public function load(Request $request, SalesChannelContext $context): CheckoutConfirmPage
    {
        $page = $this->genericPageLoader->load($request$context);
        $page = CheckoutConfirmPage::createFrom($page);

        if ($page->getMetaInformation()) {
            $page->getMetaInformation()->setRobots('noindex,follow');
        }

        $page->setPaymentMethods($this->getPaymentMethods($context));
        $page->setShippingMethods($this->getShippingMethods($context));

        $cart = $this->cartService->get($context->getToken()$context, false, true);
        $this->validateCustomerAddresses($cart$context);
        $page->setCart($cart);

        $page->setShowRevocation($cart->getLineItems()->hasLineItemWithState(State::IS_DOWNLOAD));
        $page->setHideShippingAddress(!$cart->getLineItems()->hasLineItemWithState(State::IS_PHYSICAL));

        $this->eventDispatcher->dispatch(
            new CheckoutConfirmPageLoadedEvent($page$context$request)
        );

        


    private function getShippingMethodToChangeTo(ErrorCollection $errors, SalesChannelContext $salesChannelContext): ?ShippingMethodEntity
    {
        $blockedShippingMethodNames = $errors->fmap(static fn (Error $error) => $error instanceof ShippingMethodBlockedError ? $error->getName() : null);

        $request = new Request(['onlyAvailable' => true]);
        $defaultShippingMethod = $this->shippingMethodRoute->load(
            $request,
            $salesChannelContext,
            new Criteria([$salesChannelContext->getSalesChannel()->getShippingMethodId()])
        )->getShippingMethods()->first();

        if ($defaultShippingMethod !== null && !\in_array($defaultShippingMethod->getName()$blockedShippingMethodNames, true)) {
            return $defaultShippingMethod;
        }

        // Default excluded take next shipping method         $criteria = new Criteria();
        $criteria->addFilter(
            new NandFilter([
                new EqualsAnyFilter('name', $blockedShippingMethodNames),
            ]),
        );
public function getDecorated(): AbstractShippingMethodRoute
    {
        return $this->decorated;
    }

    #[Route(path: '/store-api/shipping-method', name: 'store-api.shipping.method', methods: ['GET', 'POST'], defaults: ['_entity' => 'shipping_method'])]     public function load(Request $request, SalesChannelContext $context, Criteria $criteria): ShippingMethodRouteResponse
    {
        $response = $this->getDecorated()->load($request$context$criteria);

        $response->getShippingMethods()->sortShippingMethodsByPreference($context);

        $this->scriptExecutor->execute(new ShippingMethodRouteHook(
            $response->getShippingMethods(),
            $request->query->getBoolean('onlyAvailable') || $request->request->getBoolean('onlyAvailable'),
            $context
        ));

        return $response;
    }
}

        $this->sortedRoute = new SortedShippingMethodRoute($this->decorated, $this->executor);
    }

    public function testTriggersScriptHookExecution(): void
    {
        $this->decorated
            ->expects(static::once())
            ->method('load')
            ->willReturn($this->response);

        $this->executor->method('execute')->with(static::callback(fn (ShippingMethodRouteHook $hook) => $hook->getCollection() === $this->response->getShippingMethods()
            && $hook->getSalesChannelContext() === $this->context
            && $hook->isOnlyAvailable()));

        $response = $this->sortedRoute->load(new Request(['onlyAvailable' => true])$this->context, new Criteria());
        static::assertCount(1, $response->getShippingMethods());
    }
}
Home | Imprint | This part of the site doesn't use cookies.