custom/plugins/TemplaidInvoices/src/Subscriber/PageSubscriber.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace TemplaidInvoices\Subscriber;
  3. use CoeWishlistSw6\Core\Content\Wishlist\Aggregate\WishlistNoteEntity;
  4. use CoeWishlistSw6\Storefront\Page\Wishlist\WishlistPageLoadedEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use TemplaidInvoices\Core\Content\Wishlist\WishlistItemEntity;
  7. use TemplaidInvoices\Service\WishlistService;
  8. class PageSubscriber implements EventSubscriberInterface
  9. {
  10.     private WishlistService $wishlistService;
  11.     public function __construct(
  12.         WishlistService $wishlistService
  13.     )
  14.     {
  15.         $this->wishlistService $wishlistService;
  16.     }
  17.     /**
  18.      * @return array|string[]
  19.      */
  20.     public static function getSubscribedEvents()
  21.     {
  22.         return [
  23.             WishlistPageLoadedEvent::class => 'onWishlistPageLoaded'
  24.         ];
  25.     }
  26.     /**
  27.      * @param WishlistPageLoadedEvent $event
  28.      */
  29.     public function onWishlistPageLoaded(WishlistPageLoadedEvent $event): void
  30.     {
  31.         if (!$event->getSalesChannelContext()->getCustomer()) {
  32.             return;
  33.         }
  34.         $sortedItems $this->wishlistService->getSortedWishlistItems($event->getSalesChannelContext()->getCustomerId(), $event->getContext());
  35.         $sortedIds array_values($sortedItems->map(function (WishlistItemEntity $item) {
  36.             return $item->getLineId();
  37.         }));
  38.         $sortedNotes $event->getPage()->getActiveList()->getNotes();
  39.         $sortedNotes->sort(function (WishlistNoteEntity $aWishlistNoteEntity $b) use ($sortedIds) {
  40.             $positionA array_search($a->getId(), $sortedIds) ?: 0;
  41.             $positionB array_search($b->getId(), $sortedIds) ?: 0;
  42.             return $positionA <=> $positionB;
  43.         });
  44.     }
  45. }