<?php declare(strict_types=1);
namespace SmsBecoTechnicTheme\Subscriber;
use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Content\Category\Service\NavigationLoader;
use Shopware\Core\Content\Category\Service\NavigationLoaderInterface;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\PlatformRequest;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceInterface;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceParameters;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Navigation\NavigationPageLoader;
use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class FooterPageletSubscriber implements EventSubscriberInterface
{
/**
* @var NavigationLoader
*/
private $navigationLoader;
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var SalesChannelContextServiceInterface
*/
private $salesChannelContext;
/**
* @var EntityRepositoryInterface
*/
private $entityRepository;
/**
* @var EntityRepositoryInterface
*/
private $customerGroupRepository;
public function __construct(
NavigationLoader $navigationLoader,
SystemConfigService $configService,
SalesChannelContextServiceInterface $salesChannelContext,
EntityRepositoryInterface $entityRepository,
EntityRepositoryInterface $customerGroupRepository
)
{
$this->navigationLoader = $navigationLoader;
$this->systemConfigService = $configService;
$this->salesChannelContext = $salesChannelContext;
$this->entityRepository = $entityRepository;
$this->customerGroupRepository = $customerGroupRepository;
}
public static function getSubscribedEvents(): array
{
return [
FooterPageletLoadedEvent::class => 'addCustomNavigationAndCustomerGroup',
];
}
public function addCustomNavigationAndCustomerGroup(FooterPageletLoadedEvent $event): void
{
$contextToken = $event->getRequest()->headers->get(PlatformRequest::HEADER_CONTEXT_TOKEN);
$salesChannelId = $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_ID);
$salesChannelContextParameters = new SalesChannelContextServiceParameters($salesChannelId, $contextToken, $event->getRequest()->headers->get(PlatformRequest::HEADER_LANGUAGE_ID));
$context = $this->salesChannelContext->get(
$salesChannelContextParameters
)->getContext();
$footerCategoryId = $this->systemConfigService->get('SmsBecoTechnicTheme.config.footerCategory');
if (!is_null($footerCategoryId)) {
$criteria = new Criteria([$footerCategoryId]);
$criteria->addAssociation('children');
/** @var EntitySearchResult $category */
$result = $this->entityRepository->search(
$criteria,
$context
);
$event->getPagelet()->addExtension('footerBottomCategory', $result->first());
}
//add current Customer Group
if ($event->getSalesChannelContext()->getCustomer()) {
$customerGroupId = $event->getSalesChannelContext()->getCustomer()->getGroupId();
$customerGroup = $this->customerGroupRepository->search((new Criteria([$customerGroupId])), $event->getContext());
$event->getSalesChannelContext()->getCustomer()->addExtension('currentCustomerGroup', $customerGroup->first());
}
}
}