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

  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\Log\Package;
  11. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  12. use Shopware\Core\System\Country\CountryCollection;
  13. use Shopware\Core\System\Country\SalesChannel\AbstractCountryRoute;
  14. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  15. use Shopware\Storefront\Checkout\Cart\SalesChannel\StorefrontCartFacade;
  16. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. use Symfony\Component\HttpFoundation\Request;
  19. /**
  20.  * Do not use direct or indirect repository calls in a PageLoader. Always use a store-api route to get or put data.
  21.  */
  22. #[Package('storefront')]
  23. class CheckoutCartPageLoader
  24. {
  25.     /**
  26.      * @internal
  27.      */
  28.     public function __construct(
  29.         private readonly GenericPageLoaderInterface $genericLoader,
  30.         private readonly EventDispatcherInterface $eventDispatcher,
  31.         private readonly StorefrontCartFacade $cartService,
  32.         private readonly AbstractPaymentMethodRoute $paymentMethodRoute,
  33.         private readonly AbstractShippingMethodRoute $shippingMethodRoute,
  34.         private readonly AbstractCountryRoute $countryRoute
  35.     ) {
  36.     }
  37.     /**
  38.      * @throws CategoryNotFoundException
  39.      * @throws InconsistentCriteriaIdsException
  40.      * @throws MissingRequestParameterException
  41.      */
  42.     public function load(Request $requestSalesChannelContext $salesChannelContext): CheckoutCartPage
  43.     {
  44.         $page $this->genericLoader->load($request$salesChannelContext);
  45.         $page CheckoutCartPage::createFrom($page);
  46.         if ($page->getMetaInformation()) {
  47.             $page->getMetaInformation()->setRobots('noindex,follow');
  48.         }
  49.         $page->setCountries($this->getCountries($salesChannelContext));
  50.         $page->setPaymentMethods($this->getPaymentMethods($salesChannelContext));
  51.         $page->setShippingMethods($this->getShippingMethods($salesChannelContext));
  52.         $page->setCart($this->cartService->get($salesChannelContext->getToken(), $salesChannelContext));
  53.         $this->eventDispatcher->dispatch(
  54.             new CheckoutCartPageLoadedEvent($page$salesChannelContext$request)
  55.         );
  56.         return $page;
  57.     }
  58.     private function getPaymentMethods(SalesChannelContext $context): PaymentMethodCollection
  59.     {
  60.         $request = new Request();
  61.         $request->query->set('onlyAvailable''1');
  62.         return $this->paymentMethodRoute->load($request$context, new Criteria())->getPaymentMethods();
  63.     }
  64.     private function getShippingMethods(SalesChannelContext $context): ShippingMethodCollection
  65.     {
  66.         $request = new Request();
  67.         $request->query->set('onlyAvailable''1');
  68.         $shippingMethods $this->shippingMethodRoute
  69.             ->load($request$context, new Criteria())
  70.             ->getShippingMethods();
  71.         if (!$shippingMethods->has($context->getShippingMethod()->getId())) {
  72.             $shippingMethods->add($context->getShippingMethod());
  73.         }
  74.         return $shippingMethods;
  75.     }
  76.     private function getCountries(SalesChannelContext $context): CountryCollection
  77.     {
  78.         $countries $this->countryRoute->load(new Request(), new Criteria(), $context)->getCountries();
  79.         $countries->sortByPositionAndName();
  80.         return $countries;
  81.     }
  82. }