vendor/shopware/platform/src/Core/Content/Flow/Dispatching/Action/SetOrderCustomFieldAction.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\Order\OrderEntity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\Event\FlowEvent;
  8. use Shopware\Core\Framework\Event\OrderAware;
  9. class SetOrderCustomFieldAction extends FlowAction
  10. {
  11.     use CustomFieldActionTrait;
  12.     private Connection $connection;
  13.     private EntityRepositoryInterface $orderRepository;
  14.     public function __construct(
  15.         Connection $connection,
  16.         EntityRepositoryInterface $orderRepository
  17.     ) {
  18.         $this->connection $connection;
  19.         $this->orderRepository $orderRepository;
  20.     }
  21.     public static function getName(): string
  22.     {
  23.         return 'action.set.order.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 [OrderAware::class];
  34.     }
  35.     public function handle(FlowEvent $event): void
  36.     {
  37.         $baseEvent $event->getEvent();
  38.         if (!$baseEvent instanceof OrderAware) {
  39.             return;
  40.         }
  41.         $config $event->getConfig();
  42.         $orderId $baseEvent->getOrderId();
  43.         /** @var OrderEntity $order */
  44.         $order $this->orderRepository->search(new Criteria([$orderId]), $baseEvent->getContext())->first();
  45.         $customFields $this->getCustomFieldForUpdating($order->getCustomfields(), $config);
  46.         if ($customFields === null) {
  47.             return;
  48.         }
  49.         $customFields = empty($customFields) ? null $customFields;
  50.         $this->orderRepository->update([
  51.             [
  52.                 'id' => $orderId,
  53.                 'customFields' => $customFields,
  54.             ],
  55.         ], $baseEvent->getContext());
  56.     }
  57. }