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

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