custom/plugins/WbfkExtensions/src/Subscriber/AddB2BCustomerGroupFieldValidation.php line 28

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\Framework\Validation\BuildValidationEvent;
  6. use Shopware\Core\System\SystemConfig\SystemConfigService;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Validator\Constraints\Choice;
  9. class AddB2BCustomerGroupFieldValidation implements EventSubscriberInterface
  10. {
  11.     public function __construct(private readonly SystemConfigService $systemConfigService)
  12.     {
  13.     }
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             'framework.validation.customer.create' => 'makeCustomerGroupRequiredIfCustomerIsCompany',
  18.             'customer.profile.update' => 'makeCustomerGroupRequiredIfCustomerIsCompany',
  19.         ];
  20.     }
  21.     public function makeCustomerGroupRequiredIfCustomerIsCompany(BuildValidationEvent $event): void
  22.     {
  23.         $accountType $event->getData()->get('accountType'CustomerEntity::ACCOUNT_TYPE_PRIVATE);
  24.         if ($accountType === CustomerEntity::ACCOUNT_TYPE_BUSINESS) {
  25.             // Resets shopware's default validation for requestedGroupId and add validation to make sure
  26.             // requested customer group id is one of b2bCustomerGroupIds
  27.             $event->getDefinition()->set(
  28.                 'requestedGroupId',
  29.                 new Choice($this->systemConfigService->get('WbfkExtensions.config.b2bCustomerGroupIds'))
  30.             );
  31.         }
  32.     }
  33. }