vendor/shopware/core/Content/Flow/Dispatching/Action/SetCustomerCustomFieldAction.php line 45

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Flow\Dispatching\Action;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\Event\CustomerAware;
  8. use Shopware\Core\Framework\Event\FlowEvent;
  9. class SetCustomerCustomFieldAction extends FlowAction
  10. {
  11.     use CustomFieldActionTrait;
  12.     private Connection $connection;
  13.     private EntityRepositoryInterface $customerRepository;
  14.     public function __construct(
  15.         Connection $connection,
  16.         EntityRepositoryInterface $customerRepository
  17.     ) {
  18.         $this->connection $connection;
  19.         $this->customerRepository $customerRepository;
  20.     }
  21.     public static function getName(): string
  22.     {
  23.         return 'action.set.customer.custom.field';
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             self::getName() => 'handle',
  29.         ];
  30.     }
  31.     public function requirements(): array
  32.     {
  33.         return [CustomerAware::class];
  34.     }
  35.     public function handle(FlowEvent $event): void
  36.     {
  37.         $baseEvent $event->getEvent();
  38.         if (!$baseEvent instanceof CustomerAware) {
  39.             return;
  40.         }
  41.         $config $event->getConfig();
  42.         $customerId $baseEvent->getCustomerId();
  43.         /** @var CustomerEntity $customer */
  44.         $customer $this->customerRepository->search(new Criteria([$customerId]), $baseEvent->getContext())->first();
  45.         $customFields $this->getCustomFieldForUpdating($customer->getCustomfields(), $config);
  46.         if ($customFields === null) {
  47.             return;
  48.         }
  49.         $customFields = empty($customFields) ? null $customFields;
  50.         $this->customerRepository->update([
  51.             [
  52.                 'id' => $customerId,
  53.                 'customFields' => $customFields,
  54.             ],
  55.         ], $baseEvent->getContext());
  56.     }
  57. }