vendor/shopware/storefront/Page/Checkout/Offcanvas/OffcanvasCartPageLoader.php line 49

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Page\Checkout\Offcanvas;
  3. use Shopware\Core\Checkout\Shipping\SalesChannel\AbstractShippingMethodRoute;
  4. use Shopware\Core\Checkout\Shipping\ShippingMethodCollection;
  5. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\Log\Package;
  9. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Storefront\Checkout\Cart\SalesChannel\StorefrontCartFacade;
  12. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. #[Package('storefront')]
  16. class OffcanvasCartPageLoader
  17. {
  18.     private EventDispatcherInterface $eventDispatcher;
  19.     private StorefrontCartFacade $cartService;
  20.     private GenericPageLoaderInterface $genericLoader;
  21.     private AbstractShippingMethodRoute $shippingMethodRoute;
  22.     /**
  23.      * @internal
  24.      */
  25.     public function __construct(
  26.         EventDispatcherInterface $eventDispatcher,
  27.         StorefrontCartFacade $cartService,
  28.         GenericPageLoaderInterface $genericLoader,
  29.         AbstractShippingMethodRoute $shippingMethodRoute
  30.     ) {
  31.         $this->eventDispatcher $eventDispatcher;
  32.         $this->cartService $cartService;
  33.         $this->genericLoader $genericLoader;
  34.         $this->shippingMethodRoute $shippingMethodRoute;
  35.     }
  36.     /**
  37.      * @throws CategoryNotFoundException
  38.      * @throws InconsistentCriteriaIdsException
  39.      * @throws MissingRequestParameterException
  40.      */
  41.     public function load(Request $requestSalesChannelContext $salesChannelContext): OffcanvasCartPage
  42.     {
  43.         $page $this->genericLoader->load($request$salesChannelContext);
  44.         $page OffcanvasCartPage::createFrom($page);
  45.         if ($page->getMetaInformation()) {
  46.             $page->getMetaInformation()->assign(['robots' => 'noindex,follow']);
  47.         }
  48.         $page->setCart($this->cartService->get($salesChannelContext->getToken(), $salesChannelContext));
  49.         $page->setShippingMethods($this->getShippingMethods($salesChannelContext));
  50.         $this->eventDispatcher->dispatch(
  51.             new OffcanvasCartPageLoadedEvent($page$salesChannelContext$request)
  52.         );
  53.         return $page;
  54.     }
  55.     private function getShippingMethods(SalesChannelContext $context): ShippingMethodCollection
  56.     {
  57.         $request = new Request();
  58.         $request->query->set('onlyAvailable''1');
  59.         return $this->shippingMethodRoute->load($request$context, new Criteria())->getShippingMethods();
  60.     }
  61. }