vendor/store.shopware.com/coewishlistsw6/src/Core/Checkout/Customer/Subscriber/CustomerLoadedSubscriber.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace CoeWishlistSw6\Core\Checkout\Customer\Subscriber;
  3. use CoeWishlistSw6\Core\Content\Wishlist\Service\WishlistService;
  4. use CoeWishlistSw6\Core\Content\Wishlist\Service\WishlistServiceInterface;
  5. use Shopware\Core\Checkout\Customer\CustomerEntity;
  6. use Shopware\Core\Checkout\Customer\CustomerEvents;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  8. use Shopware\Core\Framework\Struct\ArrayEntity;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class CustomerLoadedSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var WishlistServiceInterface
  14.      */
  15.     private $wishlistService;
  16.     /**
  17.      * CustomerLoadedSubscriber constructor.
  18.      * @param WishlistServiceInterface $wishlistService
  19.      */
  20.     public function __construct(
  21.         WishlistServiceInterface $wishlistService
  22.     ) {
  23.         $this->wishlistService $wishlistService;
  24.     }
  25.     /**
  26.      * @return array
  27.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  28.      */
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             CustomerEvents::CUSTOMER_LOADED_EVENT => 'onCustomerLoaded',
  33.         ];
  34.     }
  35.     /**
  36.      * @param EntityLoadedEvent $event
  37.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  38.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  39.      */
  40.     public function onCustomerLoaded(EntityLoadedEvent $event): void
  41.     {
  42.         //@todo since we just need the extension in the admin, execute it only when in admin context
  43.         /** @var CustomerEntity $customer */
  44.         foreach ($event->getEntities() as $customer) {
  45.             $hasLists $this->wishlistService->hasUserLists($customer->getId(), $event->getContext());
  46.             $customer->addExtension("hasWishlists", new ArrayEntity(["hasWishlists" => $hasLists]));
  47.         }
  48.     }
  49. }