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

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\SalesChannel\SalesChannelContext;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
  11. use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoadedEvent;
  12. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  13. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  14. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  15. use Shopware\Storefront\Page\PageLoadedEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class ProvideB2BCustomerGroupData implements EventSubscriberInterface
  18. {
  19. public function __construct(
  20. private readonly SystemConfigService $systemConfigService,
  21. private readonly EntityRepository $customerGroupRepository
  22. ) {
  23. }
  24. public static function getSubscribedEvents(): array
  25. {
  26. return [
  27. AccountLoginPageLoadedEvent::class => 'addCustomerGroupsToPage',
  28. AccountProfilePageLoadedEvent::class => 'addCustomerGroupsToPage',
  29. CheckoutConfirmPageLoadedEvent::class => 'addCustomerGroupsToPage',
  30. CheckoutCartPageLoadedEvent::class => 'addCustomerGroupsToPage',
  31. CheckoutRegisterPageLoadedEvent::class => 'addCustomerGroupsToPage',
  32. ];
  33. }
  34. public function addCustomerGroupsToPage(PageLoadedEvent $event): void
  35. {
  36. $groupsData = $this->getCustomerGroupsConfigurations($event->getSalesChannelContext());
  37. $event->getPage()->addExtension('b2bCustomerGroups', $groupsData['b2bCustomerGroups']);
  38. $event->getPage()->addExtension('b2bCustomerGroupsHiddenForGuests', new ArrayStruct($groupsData['b2bCustomerGroupsHiddenForGuests']));
  39. $event->getPage()->addExtension('autoApprovedB2BCustomerGroupIds', new ArrayStruct($groupsData['autoApprovedB2BCustomerGroupIds']));
  40. }
  41. public function getCustomerGroupsConfigurations(SalesChannelContext $scContext): array
  42. {
  43. static $ret = null;
  44. if (!$ret) {
  45. $b2bCustomerGroupIds = $this->systemConfigService->get('WbfkExtensions.config.b2bCustomerGroupIds') ?? [];
  46. $customerGroups = $this->customerGroupRepository->search(
  47. new Criteria($b2bCustomerGroupIds),
  48. $scContext->getContext()
  49. )->getEntities();
  50. $b2bCustomerGroupIdsHiddenForGuests = $this->systemConfigService->get('WbfkExtensions.config.b2bCustomerGroupIdsHiddenForGuests') ?? [];
  51. $hiddenForGuestCriteria = new Criteria();
  52. $hiddenForGuestCriteria->addFilter(new EqualsAnyFilter('id', $b2bCustomerGroupIdsHiddenForGuests));
  53. $customerGroupsHiddenForGuests = $this->customerGroupRepository->search(
  54. $hiddenForGuestCriteria,
  55. $scContext->getContext()
  56. )->getEntities();
  57. $customerGroupsHiddenForGuests = array_map(fn($group) => $group->getId(), $customerGroupsHiddenForGuests->getElements());
  58. // Only auto approved customer groups are available for guest checkout,
  59. // For this auto approved customer group ids are passed to page
  60. $autoApprovedB2BCustomerGroupIds = $this->systemConfigService->get('WbfkExtensions.config.autoApprovedB2BCustomerGroupIds') ?? [];
  61. $customer = $scContext->getCustomer();
  62. // To get the group of the current user we can not use "customer->getGroup().
  63. // The association is not loaded. But we can prevent an additional DB roundtrip when using it from the SalesChannelContext.
  64. // If no user is logged in, this is the default group.
  65. // That is way we check the user before using the SalesChannelContext group.
  66. $group = $customer ? $scContext->getCurrentCustomerGroup() : null;
  67. $ret = [
  68. 'b2bCustomerGroups' => $customerGroups,
  69. 'b2bCustomerGroupsHiddenForGuests' => $customerGroupsHiddenForGuests,
  70. 'autoApprovedB2BCustomerGroupIds' => $autoApprovedB2BCustomerGroupIds,
  71. 'currentCustomerGroupId' => $group?->getId(),
  72. 'onlyCompanyRegistration' => $group?->getRegistrationOnlyCompanyRegistration() ?? false,
  73. ];
  74. }
  75. return $ret;
  76. }
  77. }