<?php
declare(strict_types=1);
namespace WbfkExtensions\Subscriber;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Framework\Validation\BuildValidationEvent;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Validator\Constraints\Choice;
class AddB2BCustomerGroupFieldValidation implements EventSubscriberInterface
{
public function __construct(private readonly SystemConfigService $systemConfigService)
{
}
public static function getSubscribedEvents(): array
{
return [
'framework.validation.customer.create' => 'makeCustomerGroupRequiredIfCustomerIsCompany',
'customer.profile.update' => 'makeCustomerGroupRequiredIfCustomerIsCompany',
];
}
public function makeCustomerGroupRequiredIfCustomerIsCompany(BuildValidationEvent $event): void
{
$accountType = $event->getData()->get('accountType', CustomerEntity::ACCOUNT_TYPE_PRIVATE);
if ($accountType === CustomerEntity::ACCOUNT_TYPE_BUSINESS) {
// Resets shopware's default validation for requestedGroupId and add validation to make sure
// requested customer group id is one of b2bCustomerGroupIds
$event->getDefinition()->set(
'requestedGroupId',
new Choice($this->systemConfigService->get('WbfkExtensions.config.b2bCustomerGroupIds'))
);
}
}
}