<?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\SalesChannel\SalesChannelContext;
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
{
$groupsData = $this->getCustomerGroupsConfigurations($event->getSalesChannelContext());
$event->getPage()->addExtension('b2bCustomerGroups', $groupsData['b2bCustomerGroups']);
$event->getPage()->addExtension('b2bCustomerGroupsHiddenForGuests', new ArrayStruct($groupsData['b2bCustomerGroupsHiddenForGuests']));
$event->getPage()->addExtension('autoApprovedB2BCustomerGroupIds', new ArrayStruct($groupsData['autoApprovedB2BCustomerGroupIds']));
}
public function getCustomerGroupsConfigurations(SalesChannelContext $scContext): array
{
static $ret = null;
if (!$ret) {
$b2bCustomerGroupIds = $this->systemConfigService->get('WbfkExtensions.config.b2bCustomerGroupIds') ?? [];
$customerGroups = $this->customerGroupRepository->search(
new Criteria($b2bCustomerGroupIds),
$scContext->getContext()
)->getEntities();
$b2bCustomerGroupIdsHiddenForGuests = $this->systemConfigService->get('WbfkExtensions.config.b2bCustomerGroupIdsHiddenForGuests') ?? [];
$hiddenForGuestCriteria = new Criteria();
$hiddenForGuestCriteria->addFilter(new EqualsAnyFilter('id', $b2bCustomerGroupIdsHiddenForGuests));
$customerGroupsHiddenForGuests = $this->customerGroupRepository->search(
$hiddenForGuestCriteria,
$scContext->getContext()
)->getEntities();
$customerGroupsHiddenForGuests = array_map(fn($group) => $group->getId(), $customerGroupsHiddenForGuests->getElements());
// Only auto approved customer groups are available for guest checkout,
// For this auto approved customer group ids are passed to page
$autoApprovedB2BCustomerGroupIds = $this->systemConfigService->get('WbfkExtensions.config.autoApprovedB2BCustomerGroupIds') ?? [];
$customer = $scContext->getCustomer();
// To get the group of the current user we can not use "customer->getGroup().
// The association is not loaded. But we can prevent an additional DB roundtrip when using it from the SalesChannelContext.
// If no user is logged in, this is the default group.
// That is way we check the user before using the SalesChannelContext group.
$group = $customer ? $scContext->getCurrentCustomerGroup() : null;
$ret = [
'b2bCustomerGroups' => $customerGroups,
'b2bCustomerGroupsHiddenForGuests' => $customerGroupsHiddenForGuests,
'autoApprovedB2BCustomerGroupIds' => $autoApprovedB2BCustomerGroupIds,
'currentCustomerGroupId' => $group?->getId(),
'onlyCompanyRegistration' => $group?->getRegistrationOnlyCompanyRegistration() ?? false,
];
}
return $ret;
}
}