vendor/shopware/storefront/Page/Checkout/Cart/CheckoutCartPageLoader.php line 61

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Page\Checkout\Cart;
  3. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  4. use Shopware\Core\Checkout\Payment\SalesChannel\AbstractPaymentMethodRoute;
  5. use Shopware\Core\Checkout\Shipping\SalesChannel\AbstractShippingMethodRoute;
  6. use Shopware\Core\Checkout\Shipping\ShippingMethodCollection;
  7. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  11. use Shopware\Core\System\Country\CountryCollection;
  12. use Shopware\Core\System\Country\SalesChannel\AbstractCountryRoute;
  13. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  14. use Shopware\Storefront\Checkout\Cart\SalesChannel\StorefrontCartFacade;
  15. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. class CheckoutCartPageLoader
  19. {
  20.     private GenericPageLoaderInterface $genericLoader;
  21.     private EventDispatcherInterface $eventDispatcher;
  22.     private StorefrontCartFacade $cartService;
  23.     private AbstractPaymentMethodRoute $paymentMethodRoute;
  24.     private AbstractShippingMethodRoute $shippingMethodRoute;
  25.     private AbstractCountryRoute $countryRoute;
  26.     /**
  27.      * @internal
  28.      */
  29.     public function __construct(
  30.         GenericPageLoaderInterface $genericLoader,
  31.         EventDispatcherInterface $eventDispatcher,
  32.         StorefrontCartFacade $cartService,
  33.         AbstractPaymentMethodRoute $paymentMethodRoute,
  34.         AbstractShippingMethodRoute $shippingMethodRoute,
  35.         AbstractCountryRoute $countryRoute
  36.     ) {
  37.         $this->genericLoader $genericLoader;
  38.         $this->eventDispatcher $eventDispatcher;
  39.         $this->cartService $cartService;
  40.         $this->paymentMethodRoute $paymentMethodRoute;
  41.         $this->shippingMethodRoute $shippingMethodRoute;
  42.         $this->countryRoute $countryRoute;
  43.     }
  44.     /**
  45.      * @throws CategoryNotFoundException
  46.      * @throws InconsistentCriteriaIdsException
  47.      * @throws MissingRequestParameterException
  48.      */
  49.     public function load(Request $requestSalesChannelContext $salesChannelContext): CheckoutCartPage
  50.     {
  51.         $page $this->genericLoader->load($request$salesChannelContext);
  52.         $page CheckoutCartPage::createFrom($page);
  53.         if ($page->getMetaInformation()) {
  54.             $page->getMetaInformation()->setRobots('noindex,follow');
  55.         }
  56.         $page->setCountries($this->getCountries($salesChannelContext));
  57.         $page->setPaymentMethods($this->getPaymentMethods($salesChannelContext));
  58.         $page->setShippingMethods($this->getShippingMethods($salesChannelContext));
  59.         $page->setCart($this->cartService->get($salesChannelContext->getToken(), $salesChannelContext));
  60.         $this->eventDispatcher->dispatch(
  61.             new CheckoutCartPageLoadedEvent($page$salesChannelContext$request)
  62.         );
  63.         return $page;
  64.     }
  65.     private function getPaymentMethods(SalesChannelContext $context): PaymentMethodCollection
  66.     {
  67.         $request = new Request();
  68.         $request->query->set('onlyAvailable''1');
  69.         return $this->paymentMethodRoute->load($request$context, new Criteria())->getPaymentMethods();
  70.     }
  71.     private function getShippingMethods(SalesChannelContext $context): ShippingMethodCollection
  72.     {
  73.         $request = new Request();
  74.         $request->query->set('onlyAvailable''1');
  75.         $shippingMethods $this->shippingMethodRoute
  76.             ->load($request$context, new Criteria())
  77.             ->getShippingMethods();
  78.         if (!$shippingMethods->has($context->getShippingMethod()->getId())) {
  79.             $shippingMethods->add($context->getShippingMethod());
  80.         }
  81.         return $shippingMethods;
  82.     }
  83.     private function getCountries(SalesChannelContext $context): CountryCollection
  84.     {
  85.         $countries $this->countryRoute->load(new Request(), new Criteria(), $context)->getCountries();
  86.         $countries->sortByPositionAndName();
  87.         return $countries;
  88.     }
  89. }