custom/plugins/WbfkExtensions/src/Subscriber/ProvideB2BCustomerGroupData.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WbfkExtensions\Subscriber;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  7. use Shopware\Core\Framework\Struct\ArrayStruct;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
  10. use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoadedEvent;
  11. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  12. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  13. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  14. use Shopware\Storefront\Page\PageLoadedEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class ProvideB2BCustomerGroupData implements EventSubscriberInterface
  17. {
  18.     public function __construct(
  19.         private readonly SystemConfigService $systemConfigService,
  20.         private readonly EntityRepository $customerGroupRepository
  21.     ) {
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             AccountLoginPageLoadedEvent::class => 'addCustomerGroupsToPage',
  27.             AccountProfilePageLoadedEvent::class => 'addCustomerGroupsToPage',
  28.             CheckoutConfirmPageLoadedEvent::class => 'addCustomerGroupsToPage',
  29.             CheckoutCartPageLoadedEvent::class => 'addCustomerGroupsToPage',
  30.             CheckoutRegisterPageLoadedEvent::class => 'addCustomerGroupsToPage',
  31.         ];
  32.     }
  33.     public function addCustomerGroupsToPage(PageLoadedEvent $event): void
  34.     {
  35.         $b2bCustomerGroupIds $this->systemConfigService->get('WbfkExtensions.config.b2bCustomerGroupIds') ?? [];
  36.         $customerGroups $this->customerGroupRepository->search(
  37.             new Criteria($b2bCustomerGroupIds),
  38.             $event->getContext()
  39.         )->getEntities();
  40.         $b2bCustomerGroupIdsHiddenForGuests $this->systemConfigService->get('WbfkExtensions.config.b2bCustomerGroupIdsHiddenForGuests') ?? [];
  41.         $hiddenForGuestCriteria = new Criteria();
  42.         $hiddenForGuestCriteria->addFilter(new EqualsAnyFilter('id'$b2bCustomerGroupIdsHiddenForGuests));
  43.         $customerGroupsHiddenForGuests $this->customerGroupRepository->search(
  44.             $hiddenForGuestCriteria,
  45.             $event->getContext()
  46.         )->getEntities();
  47.         $customerGroupsHiddenForGuests array_map(fn ($group) => $group->getId(), $customerGroupsHiddenForGuests->getElements());
  48.         $customerGroupsHiddenForGuests = new ArrayStruct($customerGroupsHiddenForGuests);
  49.         /** @noinspection PhpDeprecationInspection */
  50.         $event->getPage()->addExtension('b2bCustomerGroups'$customerGroups);
  51.         /** @noinspection PhpDeprecationInspection */
  52.         $event->getPage()->addExtension('b2bCustomerGroupsHiddenForGuests'$customerGroupsHiddenForGuests);
  53.         // Only auto approved customer groups are available for guest checkout,
  54.         // For this auto approved customer group ids are passed to page
  55.         $autoApprovedB2BCustomerGroupIds = new ArrayStruct($this->systemConfigService->get(
  56.             'WbfkExtensions.config.autoApprovedB2BCustomerGroupIds'
  57.         ) ?? []);
  58.         /** @noinspection PhpDeprecationInspection */
  59.         $event->getPage()->addExtension('autoApprovedB2BCustomerGroupIds'$autoApprovedB2BCustomerGroupIds);
  60.     }
  61. }