<?php declare(strict_types=1);
namespace CoeWishlistSw6\Core\Checkout\Customer\Subscriber;
use CoeWishlistSw6\Core\Content\Wishlist\Service\WishlistService;
use CoeWishlistSw6\Core\Content\Wishlist\Service\WishlistServiceInterface;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CustomerLoadedSubscriber implements EventSubscriberInterface
{
/**
* @var WishlistServiceInterface
*/
private $wishlistService;
/**
* CustomerLoadedSubscriber constructor.
* @param WishlistServiceInterface $wishlistService
*/
public function __construct(
WishlistServiceInterface $wishlistService
) {
$this->wishlistService = $wishlistService;
}
/**
* @return array
* @author Jeffry Block <jeffry.block@codeenterprise.de>
*/
public static function getSubscribedEvents(): array
{
return [
CustomerEvents::CUSTOMER_LOADED_EVENT => 'onCustomerLoaded',
];
}
/**
* @param EntityLoadedEvent $event
* @author Jeffry Block <jeffry.block@codeenterprise.de>
* @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
*/
public function onCustomerLoaded(EntityLoadedEvent $event): void
{
//@todo since we just need the extension in the admin, execute it only when in admin context
/** @var CustomerEntity $customer */
foreach ($event->getEntities() as $customer) {
$hasLists = $this->wishlistService->hasUserLists($customer->getId(), $event->getContext());
$customer->addExtension("hasWishlists", new ArrayEntity(["hasWishlists" => $hasLists]));
}
}
}