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

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 Shopware\Core\Content\Property\PropertyGroupEntity;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use TemplaidInvoices\Core\Content\Wishlist\WishlistItemEntity;
  12. use TemplaidInvoices\Service\WishlistService;
  13. class PageSubscriber implements EventSubscriberInterface
  14. {
  15.     private WishlistService $wishlistService;
  16.     private EntityRepository $propertyGroupRepository;
  17.     private SystemConfigService $configService;
  18.     public function __construct(
  19.         WishlistService          $wishlistService,
  20.         EntityRepository         $propertyGroupRepository,
  21.         SystemConfigService      $configService
  22.     )
  23.     {
  24.         $this->wishlistService $wishlistService;
  25.         $this->propertyGroupRepository $propertyGroupRepository;
  26.         $this->configService $configService;
  27.     }
  28.     /**
  29.      * @return array|string[]
  30.      */
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             WishlistPageLoadedEvent::class => 'onWishlistPageLoaded',
  35.             GenericPageLoadedEvent::class => 'onAccountPageLoadedEvent'
  36.         ];
  37.     }
  38.     /**
  39.      * @param GenericPageLoadedEvent $event
  40.      */
  41.     public function onAccountPageLoadedEvent(GenericPageLoadedEvent $event): void
  42.     {
  43.         if (!$event->getSalesChannelContext()->getCustomer() || !str_contains($event->getRequest()->getPathInfo(), '/account')) {
  44.             return;
  45.         }
  46.         $lists $this->wishlistService->getCustomerLists($event->getSalesChannelContext());
  47.         $event->getPage()->addArrayExtension('assortmentLists'$lists);
  48.         if ($assortmentPropertyIds $this->configService->get('TemplaidInvoices.config.assortmentProperties')) {
  49.             $criteria = new Criteria($assortmentPropertyIds);
  50.             $properties $this->propertyGroupRepository->search($criteria$event->getContext())->map(function (PropertyGroupEntity $property) {
  51.                 return [
  52.                     'id' => $property->getId(),
  53.                     'label' => $property->getName()
  54.                 ];
  55.             });
  56.             $event->getPage()->addArrayExtension('assortmentProperties'$properties);
  57.         }
  58.     }
  59.     /**
  60.      * @param WishlistPageLoadedEvent $event
  61.      */
  62.     public function onWishlistPageLoaded(WishlistPageLoadedEvent $event): void
  63.     {
  64.         if (!$event->getSalesChannelContext()->getCustomer()) {
  65.             return;
  66.         }
  67.         $sortedItems $this->wishlistService->getSortedWishlistItems($event->getSalesChannelContext()->getCustomerId(), $event->getContext());
  68.         $sortedIds array_values($sortedItems->map(function (WishlistItemEntity $item) {
  69.             return $item->getLineId();
  70.         }));
  71.         $sortedNotes $event->getPage()->getActiveList()->getNotes();
  72.         $sortedNotes->sort(function (WishlistNoteEntity $aWishlistNoteEntity $b) use ($sortedIds) {
  73.             $positionA array_search($a->getId(), $sortedIds) ?: 0;
  74.             $positionB array_search($b->getId(), $sortedIds) ?: 0;
  75.             return $positionA <=> $positionB;
  76.         });
  77.     }
  78. }