<?php declare(strict_types=1);
namespace AbasBecoCustomizations\Subscribers\Page;
use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
class PageSubscriber implements EventSubscriberInterface
{
private EntityRepository $customerGroupRepository;
public function __construct(EntityRepository $customerGroupRepository)
{
$this->customerGroupRepository = $customerGroupRepository;
}
public static function getSubscribedEvents()
{
return [
PageLoadedEvent::class => 'handlePageLoadedEvent',
ProductPageLoadedEvent::class => 'handlePageLoadedEvent',
ProductPageCriteriaEvent::class => 'handleProductPageCriteriaEvent'
];
}
public function handlePageLoadedEvent(PageLoadedEvent $event)
{
$context = $event->getSalesChannelContext();
$customer = $context->getCustomer();
if (!is_null($customer)) {
$customerGroup = $this->customerGroupRepository->search((new Criteria([$customer->getGroupId()])), $event->getContext())->first();
$customer->setGroup($customerGroup);
}
}
public function handleProductPageCriteriaEvent(ProductPageCriteriaEvent $event)
{
$event->getCriteria()->addAssociation('abasSupplier.country');
$event->getCriteria()->addAssociation('abasSupplier.countryState');
$event->getCriteria()->addAssociation('abasSupplierContact.salutation');
$event->getCriteria()->addAssociation('abasSupplierContact.country');
$event->getCriteria()->addAssociation('abasSupplierContact.countryState');
}
}