vendor/shopware/storefront/Page/Account/Order/AccountOrderPageLoader.php line 60

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Page\Account\Order;
  3. use Shopware\Core\Checkout\Cart\CartException;
  4. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  5. use Shopware\Core\Checkout\Customer\SalesChannel\AccountService;
  6. use Shopware\Core\Checkout\Order\Exception\GuestNotAuthenticatedException;
  7. use Shopware\Core\Checkout\Order\Exception\WrongGuestCredentialsException;
  8. use Shopware\Core\Checkout\Order\SalesChannel\AbstractOrderRoute;
  9. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  15. use Shopware\Core\Framework\Log\Package;
  16. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  17. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  18. use Shopware\Storefront\Event\RouteRequest\OrderRouteRequestEvent;
  19. use Shopware\Storefront\Framework\Page\StorefrontSearchResult;
  20. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  21. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  22. use Symfony\Component\HttpFoundation\Request;
  23. #[Package('customer-order')]
  24. class AccountOrderPageLoader
  25. {
  26.     private GenericPageLoaderInterface $genericLoader;
  27.     private EventDispatcherInterface $eventDispatcher;
  28.     private AbstractOrderRoute $orderRoute;
  29.     private AccountService $accountService;
  30.     /**
  31.      * @internal
  32.      */
  33.     public function __construct(
  34.         GenericPageLoaderInterface $genericLoader,
  35.         EventDispatcherInterface $eventDispatcher,
  36.         AbstractOrderRoute $orderRoute,
  37.         AccountService $accountService
  38.     ) {
  39.         $this->genericLoader $genericLoader;
  40.         $this->eventDispatcher $eventDispatcher;
  41.         $this->orderRoute $orderRoute;
  42.         $this->accountService $accountService;
  43.     }
  44.     /**
  45.      * @throws CategoryNotFoundException
  46.      * @throws CustomerNotLoggedInException
  47.      * @throws GuestNotAuthenticatedException
  48.      * @throws InconsistentCriteriaIdsException
  49.      * @throws MissingRequestParameterException
  50.      * @throws WrongGuestCredentialsException
  51.      */
  52.     public function load(Request $requestSalesChannelContext $salesChannelContext): AccountOrderPage
  53.     {
  54.         if (!$salesChannelContext->getCustomer() && $request->get('deepLinkCode'false) === false) {
  55.             throw CartException::customerNotLoggedIn();
  56.         }
  57.         $page $this->genericLoader->load($request$salesChannelContext);
  58.         $page AccountOrderPage::createFrom($page);
  59.         if ($page->getMetaInformation()) {
  60.             $page->getMetaInformation()->setRobots('noindex,follow');
  61.         }
  62.         $page->setOrders(StorefrontSearchResult::createFrom($this->getOrders($request$salesChannelContext)));
  63.         $page->setDeepLinkCode($request->get('deepLinkCode'));
  64.         if ($request->get('deepLinkCode') && $page->getOrders()->first() !== null) {
  65.             $this->accountService->loginById(
  66.                 $page->getOrders()->first()->getOrderCustomer()->getCustomer()->getId(),
  67.                 $salesChannelContext
  68.             );
  69.         }
  70.         $this->eventDispatcher->dispatch(
  71.             new AccountOrderPageLoadedEvent($page$salesChannelContext$request)
  72.         );
  73.         return $page;
  74.     }
  75.     /**
  76.      * @throws CustomerNotLoggedInException
  77.      * @throws GuestNotAuthenticatedException
  78.      * @throws WrongGuestCredentialsException
  79.      */
  80.     private function getOrders(Request $requestSalesChannelContext $context): EntitySearchResult
  81.     {
  82.         $criteria $this->createCriteria($request);
  83.         $apiRequest = new Request();
  84.         // Add email and zipcode for guest customer verification in order view
  85.         if ($request->get('email'false) && $request->get('zipcode'false)) {
  86.             $apiRequest->query->set('email'$request->get('email'));
  87.             $apiRequest->query->set('zipcode'$request->get('zipcode'));
  88.         }
  89.         $event = new OrderRouteRequestEvent($request$apiRequest$context$criteria);
  90.         $this->eventDispatcher->dispatch($event);
  91.         $responseStruct $this->orderRoute
  92.             ->load($event->getStoreApiRequest(), $context$criteria);
  93.         return $responseStruct->getOrders();
  94.     }
  95.     private function createCriteria(Request $request): Criteria
  96.     {
  97.         $limit $request->get('limit');
  98.         $limit $limit ? (int) $limit 10;
  99.         $page $request->get('p');
  100.         $page $page ? (int) $page 1;
  101.         $criteria = (new Criteria())
  102.             ->addSorting(new FieldSorting('order.createdAt'FieldSorting::DESCENDING))
  103.             ->addAssociation('transactions.paymentMethod')
  104.             ->addAssociation('deliveries.shippingMethod')
  105.             ->addAssociation('orderCustomer.customer')
  106.             ->addAssociation('lineItems')
  107.             ->addAssociation('lineItems.cover')
  108.             ->addAssociation('lineItems.downloads.media')
  109.             ->addAssociation('addresses')
  110.             ->addAssociation('currency')
  111.             ->addAssociation('documents.documentType')
  112.             ->setLimit($limit)
  113.             ->setOffset(($page 1) * $limit)
  114.             ->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_EXACT);
  115.         $criteria
  116.             ->getAssociation('transactions')
  117.             ->addSorting(new FieldSorting('createdAt'));
  118.         $criteria
  119.             ->addSorting(new FieldSorting('orderDateTime'FieldSorting::DESCENDING));
  120.         if ($request->get('deepLinkCode')) {
  121.             $criteria->addFilter(new EqualsFilter('deepLinkCode'$request->get('deepLinkCode')));
  122.         }
  123.         return $criteria;
  124.     }
  125. }