src/Storefront/Page/Checkout/Cart/CheckoutCartPageLoader.php line 49

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