custom/static-plugins/NdTheme/src/Subscriber/AccountOverviewSubscriber.php line 36

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nd\Theme\Subscriber;
  3. use Shopware\Core\Checkout\Order\SalesChannel\AbstractOrderRoute;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  5. use Shopware\Core\Framework\Struct\ArrayStruct;
  6. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  7. use Shopware\Storefront\Event\RouteRequest\OrderRouteRequestEvent;
  8. use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Symfony\Component\HttpFoundation\Request;
  13. class AccountOverviewSubscriber implements EventSubscriberInterface
  14. {
  15.     private EventDispatcherInterface $eventDispatcher;
  16.     private AbstractOrderRoute $orderRoute;
  17.     public function __construct(EventDispatcherInterface $eventDispatcherAbstractOrderRoute $orderRoute)
  18.     {
  19.         $this->eventDispatcher $eventDispatcher;
  20.         $this->orderRoute $orderRoute;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  25.         return [
  26.             AccountOverviewPageLoadedEvent::class => 'addData',
  27.         ];
  28.     }
  29.     public function addData(AccountOverviewPageLoadedEvent $event)
  30.     {
  31.         $page $event->getPage();
  32.         $newestOrders = new ArrayStruct();
  33.         $newestOrders->set('newest_orders'$this->getNewestOrders($event->getSalesChannelContext(), $event->getRequest()));
  34.         //dump($this->getNewestOrders($event->getSalesChannelContext(), $event->getRequest()));
  35.         $page->addExtension('newest_orders'$newestOrders);
  36.     }
  37.     public function getNewestOrders(SalesChannelContext $contextRequest $request ){
  38.         $criteria = (new Criteria())
  39.             ->addSorting(new FieldSorting('orderDateTime'FieldSorting::DESCENDING))
  40.             ->addAssociation('lineItems')
  41.             ->addAssociation('lineItems.cover')
  42.             ->addAssociation('transactions.paymentMethod')
  43.             ->addAssociation('deliveries.shippingMethod')
  44.             ->addAssociation('addresses')
  45.             ->addAssociation('currency')
  46.             ->addAssociation('documents.documentType')
  47.             ->setLimit(4)
  48.             ->addAssociation('orderCustomer');
  49.         $criteria->getAssociation('transactions')
  50.             ->addSorting(new FieldSorting('createdAt'));
  51.         $apiRequest = new Request();
  52.         $event = new OrderRouteRequestEvent($request$apiRequest$context$criteria);
  53.         $this->eventDispatcher->dispatch($event);
  54.         $responseStruct $this->orderRoute
  55.             ->load($event->getStoreApiRequest(), $context$criteria);
  56.         return $responseStruct->getOrders()->getEntities();
  57.     }
  58. }