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\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. #[Package('storefront')]
  20. class CheckoutCartPageLoader
  21. {
  22.     private GenericPageLoaderInterface $genericLoader;
  23.     private EventDispatcherInterface $eventDispatcher;
  24.     private StorefrontCartFacade $cartService;
  25.     private AbstractPaymentMethodRoute $paymentMethodRoute;
  26.     private AbstractShippingMethodRoute $shippingMethodRoute;
  27.     private AbstractCountryRoute $countryRoute;
  28.     /**
  29.      * @internal
  30.      */
  31.     public function __construct(
  32.         GenericPageLoaderInterface $genericLoader,
  33.         EventDispatcherInterface $eventDispatcher,
  34.         StorefrontCartFacade $cartService,
  35.         AbstractPaymentMethodRoute $paymentMethodRoute,
  36.         AbstractShippingMethodRoute $shippingMethodRoute,
  37.         AbstractCountryRoute $countryRoute
  38.     ) {
  39.         $this->genericLoader $genericLoader;
  40.         $this->eventDispatcher $eventDispatcher;
  41.         $this->cartService $cartService;
  42.         $this->paymentMethodRoute $paymentMethodRoute;
  43.         $this->shippingMethodRoute $shippingMethodRoute;
  44.         $this->countryRoute $countryRoute;
  45.     }
  46.     /**
  47.      * @throws CategoryNotFoundException
  48.      * @throws InconsistentCriteriaIdsException
  49.      * @throws MissingRequestParameterException
  50.      */
  51.     public function load(Request $requestSalesChannelContext $salesChannelContext): CheckoutCartPage
  52.     {
  53.         $page $this->genericLoader->load($request$salesChannelContext);
  54.         $page CheckoutCartPage::createFrom($page);
  55.         if ($page->getMetaInformation()) {
  56.             $page->getMetaInformation()->setRobots('noindex,follow');
  57.         }
  58.         $page->setCountries($this->getCountries($salesChannelContext));
  59.         $page->setPaymentMethods($this->getPaymentMethods($salesChannelContext));
  60.         $page->setShippingMethods($this->getShippingMethods($salesChannelContext));
  61.         $page->setCart($this->cartService->get($salesChannelContext->getToken(), $salesChannelContext));
  62.         $this->eventDispatcher->dispatch(
  63.             new CheckoutCartPageLoadedEvent($page$salesChannelContext$request)
  64.         );
  65.         return $page;
  66.     }
  67.     private function getPaymentMethods(SalesChannelContext $context): PaymentMethodCollection
  68.     {
  69.         $request = new Request();
  70.         $request->query->set('onlyAvailable''1');
  71.         return $this->paymentMethodRoute->load($request$context, new Criteria())->getPaymentMethods();
  72.     }
  73.     private function getShippingMethods(SalesChannelContext $context): ShippingMethodCollection
  74.     {
  75.         $request = new Request();
  76.         $request->query->set('onlyAvailable''1');
  77.         $shippingMethods $this->shippingMethodRoute
  78.             ->load($request$context, new Criteria())
  79.             ->getShippingMethods();
  80.         if (!$shippingMethods->has($context->getShippingMethod()->getId())) {
  81.             $shippingMethods->add($context->getShippingMethod());
  82.         }
  83.         return $shippingMethods;
  84.     }
  85.     private function getCountries(SalesChannelContext $context): CountryCollection
  86.     {
  87.         $countries $this->countryRoute->load(new Request(), new Criteria(), $context)->getCountries();
  88.         $countries->sortByPositionAndName();
  89.         return $countries;
  90.     }
  91. }