custom/plugins/WbfkExtensions/src/Subscriber/AutoApproveOrSetFallbackB2BCustomerGroup.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WbfkExtensions\Subscriber;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\Checkout\Customer\CustomerEvents;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\Event\DataMappingEvent;
  9. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class AutoApproveOrSetFallbackB2BCustomerGroup implements EventSubscriberInterface
  13. {
  14.     public function __construct(
  15.         private readonly SystemConfigService $systemConfigService,
  16.         private readonly EntityRepositoryInterface $salesChannelRepository,
  17.     ) {
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'customerGroupAssignmentWorkflow',
  23.             CustomerEvents::MAPPING_CUSTOMER_PROFILE_SAVE => 'customerGroupAssignmentWorkflow',
  24.         ];
  25.     }
  26.     public function customerGroupAssignmentWorkflow(DataMappingEvent $event): void
  27.     {
  28.         if ($event->getInput()->get('accountType') === CustomerEntity::ACCOUNT_TYPE_PRIVATE) {
  29.             $this->setDefaultCustomerGroup($event);
  30.             return;
  31.         }
  32.         $requestedCustomerGroupId $event->getInput()->get('requestedGroupId');
  33.         if (!$requestedCustomerGroupId) {
  34.             return;
  35.         }
  36.         if ($this->isAutoApprovedB2BCustomerGroup($requestedCustomerGroupId)) {
  37.             $this->autoApproveB2BCustomerGroup($event$requestedCustomerGroupId);
  38.             return;
  39.         }
  40.         $this->setFallBackB2BCustomerGroup($event);
  41.     }
  42.     private
  43.     function autoApproveB2BCustomerGroup(
  44.         DataMappingEvent $event,
  45.         string $requestedCustomerGroupId
  46.     ): void {
  47.         $output $event->getOutput();
  48.         $output['groupId'] = $requestedCustomerGroupId;
  49.         $output['requestedGroupId'] = null;
  50.         $event->setOutput($output);
  51.     }
  52.     public
  53.     function isAutoApprovedB2BCustomerGroup(
  54.         string $requestedCustomerGroupId
  55.     ): bool {
  56.         $autoApprovedB2BCustomerGroupIds $this->systemConfigService->get(
  57.             'WbfkExtensions.config.autoApprovedB2BCustomerGroupIds'
  58.         );
  59.         return in_array($requestedCustomerGroupId$autoApprovedB2BCustomerGroupIds);
  60.     }
  61.     private
  62.     function setFallBackB2BCustomerGroup(
  63.         DataMappingEvent $event
  64.     ): void {
  65.         $input $event->getInput();
  66.         $output $event->getOutput();
  67.         $output['groupId'] = $this->systemConfigService->get('WbfkExtensions.config.fallbackB2BCustomerGroupId');
  68.         $output['requestedGroupId'] = $input->get('requestedGroupId');
  69.         $event->setOutput($output);
  70.     }
  71.     private function setDefaultCustomerGroup(DataMappingEvent $event): void
  72.     {
  73.         $criteria = new Criteria([$event->getContext()->getSource()->getSalesChannelId()]);
  74.         /** @var SalesChannelEntity $salesChannel */
  75.         $salesChannel $this->salesChannelRepository->search($criteria$event->getContext())->first();
  76.         $output $event->getOutput();
  77.         $output['groupId'] = $salesChannel->getCustomerGroupId();
  78.         $output['requestedGroupId'] = null;
  79.         $event->setOutput($output);
  80.     }
  81. }