vendor/shopware/core/Content/Flow/Dispatching/Action/SetOrderCustomFieldAction.php line 49

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\DelayAware;
  8. use Shopware\Core\Framework\Event\FlowEvent;
  9. use Shopware\Core\Framework\Event\OrderAware;
  10. class SetOrderCustomFieldAction extends FlowAction
  11. {
  12.     use CustomFieldActionTrait;
  13.     private Connection $connection;
  14.     private EntityRepositoryInterface $orderRepository;
  15.     /**
  16.      * @internal
  17.      */
  18.     public function __construct(
  19.         Connection $connection,
  20.         EntityRepositoryInterface $orderRepository
  21.     ) {
  22.         $this->connection $connection;
  23.         $this->orderRepository $orderRepository;
  24.     }
  25.     public static function getName(): string
  26.     {
  27.         return 'action.set.order.custom.field';
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             self::getName() => 'handle',
  33.         ];
  34.     }
  35.     public function requirements(): array
  36.     {
  37.         return [OrderAware::class, DelayAware::class];
  38.     }
  39.     public function handle(FlowEvent $event): void
  40.     {
  41.         $baseEvent $event->getEvent();
  42.         if (!$baseEvent instanceof OrderAware) {
  43.             return;
  44.         }
  45.         $config $event->getConfig();
  46.         $orderId $baseEvent->getOrderId();
  47.         /** @var OrderEntity $order */
  48.         $order $this->orderRepository->search(new Criteria([$orderId]), $baseEvent->getContext())->first();
  49.         $customFields $this->getCustomFieldForUpdating($order->getCustomfields(), $config);
  50.         if ($customFields === null) {
  51.             return;
  52.         }
  53.         $customFields = empty($customFields) ? null $customFields;
  54.         $this->orderRepository->update([
  55.             [
  56.                 'id' => $orderId,
  57.                 'customFields' => $customFields,
  58.             ],
  59.         ], $baseEvent->getContext());
  60.     }
  61. }