custom/plugins/CoeWishlistSw6/src/Core/Checkout/Customer/Subscriber/CustomerLoadedSubscriber.php line 48

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.     {
  24.         $this->wishlistService $wishlistService;
  25.     }
  26.     /**
  27.      * @return array
  28.      * @author Jeffry Block <[email protected]>
  29.      */
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             CustomerEvents::CUSTOMER_LOADED_EVENT => 'onCustomerLoaded'
  34.         ];
  35.     }
  36.     /**
  37.      * @param EntityLoadedEvent $event
  38.      * @author Jeffry Block <[email protected]>
  39.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  40.      */
  41.     public function onCustomerLoaded(EntityLoadedEvent $event): void
  42.     {
  43.         //@todo since we just need the extension in the admin, execute it only when in admin context
  44.         /** @var CustomerEntity $customer */
  45.         foreach($event->getEntities() as $customer){
  46.             $hasLists $this->wishlistService->hasUserLists($customer->getId(), $event->getContext());
  47.             $customer->addExtension("hasWishlists", new ArrayEntity(["hasWishlists" => $hasLists]));
  48.         }
  49.     }
  50. }