<?php
declare(strict_types=1);
namespace WbfkExtensions\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProvideB2BCustomerGroupData implements EventSubscriberInterface
{
public function __construct(
private readonly SystemConfigService $systemConfigService,
private readonly EntityRepository $customerGroupRepository
) {
}
public static function getSubscribedEvents(): array
{
return [
AccountLoginPageLoadedEvent::class => 'addCustomerGroupsToPage',
AccountProfilePageLoadedEvent::class => 'addCustomerGroupsToPage',
CheckoutConfirmPageLoadedEvent::class => 'addCustomerGroupsToPage',
CheckoutCartPageLoadedEvent::class => 'addCustomerGroupsToPage',
CheckoutRegisterPageLoadedEvent::class => 'addCustomerGroupsToPage',
];
}
public function addCustomerGroupsToPage(PageLoadedEvent $event): void
{
$b2bCustomerGroupIds = $this->systemConfigService->get('WbfkExtensions.config.b2bCustomerGroupIds') ?? [];
$customerGroups = $this->customerGroupRepository->search(
new Criteria($b2bCustomerGroupIds),
$event->getContext()
)->getEntities();
$b2bCustomerGroupIdsHiddenForGuests = $this->systemConfigService->get('WbfkExtensions.config.b2bCustomerGroupIdsHiddenForGuests') ?? [];
$hiddenForGuestCriteria = new Criteria();
$hiddenForGuestCriteria->addFilter(new EqualsAnyFilter('id', $b2bCustomerGroupIdsHiddenForGuests));
$customerGroupsHiddenForGuests = $this->customerGroupRepository->search(
$hiddenForGuestCriteria,
$event->getContext()
)->getEntities();
$customerGroupsHiddenForGuests = array_map(fn ($group) => $group->getId(), $customerGroupsHiddenForGuests->getElements());
$customerGroupsHiddenForGuests = new ArrayStruct($customerGroupsHiddenForGuests);
/** @noinspection PhpDeprecationInspection */
$event->getPage()->addExtension('b2bCustomerGroups', $customerGroups);
/** @noinspection PhpDeprecationInspection */
$event->getPage()->addExtension('b2bCustomerGroupsHiddenForGuests', $customerGroupsHiddenForGuests);
// Only auto approved customer groups are available for guest checkout,
// For this auto approved customer group ids are passed to page
$autoApprovedB2BCustomerGroupIds = new ArrayStruct($this->systemConfigService->get(
'WbfkExtensions.config.autoApprovedB2BCustomerGroupIds'
) ?? []);
/** @noinspection PhpDeprecationInspection */
$event->getPage()->addExtension('autoApprovedB2BCustomerGroupIds', $autoApprovedB2BCustomerGroupIds);
}
}