custom/plugins/CioBudget/src/Subscriber/BudgetShiftOrderPlacedCustomFieldSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. namespace CioBudget\Subscriber;
  3. use CioBudget\Service\BudgetLoaderService;
  4. use CioBudget\Service\SessionService;
  5. use Shopware\Core\Checkout\Order\OrderEntity;
  6. use Shopware\Core\Checkout\Order\OrderEvents;
  7. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class BudgetShiftOrderPlacedCustomFieldSubscriber implements EventSubscriberInterface
  15. {
  16.     public const CUSTOM_FIELD_BUDGET_NAME_SAVE_ON_ORDER = 'custom_field_budget_name_save_on_order';
  17.     private EntityRepositoryInterface $paymentMethodRepository;
  18.     private BudgetLoaderService $budgetLoaderService;
  19.     private SessionService $sessionService;
  20.     private EntityRepositoryInterface $orderRepository;
  21.     public function __construct(
  22.         EntityRepositoryInterface $paymentMethodRepository,
  23.         BudgetLoaderService       $budgetLoaderService,
  24.         SessionService            $sessionService,
  25.         EntityRepositoryInterface $orderRepository
  26.     )
  27.     {
  28.         $this->paymentMethodRepository = $paymentMethodRepository;
  29.         $this->budgetLoaderService = $budgetLoaderService;
  30.         $this->sessionService = $sessionService;
  31.         $this->orderRepository = $orderRepository;
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             OrderEvents::ORDER_WRITTEN_EVENT => 'onCheckoutFinishPageLoaded'
  37.         ];
  38.     }
  39.     public function onCheckoutFinishPageLoaded(EntityWrittenEvent $event)
  40.     {
  41.         // if context is admin do nothing
  42.         if ($event->getContext()->getSource() instanceof AdminApiSource) {
  43.             return;
  44.         }
  45.         $writeResults = $event->getWriteResults();
  46.         if (!empty($writeResults) && isset($writeResults[0]) && isset($writeResults[0]->getPayload()['id'])) {
  47.             $orderId = $writeResults[0]->getPayload()['id'];
  48.             // Überprüfen, ob $orderId nicht leer ist
  49.             if (!empty($orderId)) {
  50.                 $criteria = (new Criteria())
  51.                     ->addFilter(new EqualsFilter('id', $orderId))
  52.                     ->addAssociation('transactions');
  53.                 $order = $this->orderRepository->search($criteria, $event->getContext())->first();
  54.                 if (!$order instanceof OrderEntity) {
  55.                     return;
  56.                 }
  57.                 // CIO-AI-Driven: Null-Session sauber ignorieren
  58.                 $budgetId = $this->sessionService->getBudgetIdFromSession();
  59.                 if (!is_string($budgetId) || $budgetId === '') {
  60.                     return;
  61.                 }
  62.                 $paymentMethods = $order->getTransactions() ? $order->getTransactions()->getPaymentMethodIds() : [];
  63.                 foreach ($paymentMethods as $paymentMethod) {
  64.                     /** @var string $paymentMethod */
  65.                     if ($paymentMethod === $this->getBudgetPaymentMethodId()) {
  66.                         $budget = $this->budgetLoaderService->getBudgetById($budgetId);
  67.                         if (!is_null($budget)) {
  68.                             // CIO-AI-Driven: Merge statt Ueberschreiben, First-Write-Wins (kein Update bei bestehendem Wert)
  69.                             $customFields = $order->getCustomFields() ?? [];
  70.                             if (!array_key_exists(self::CUSTOM_FIELD_BUDGET_NAME_SAVE_ON_ORDER, $customFields)) {
  71.                                 $customFields[self::CUSTOM_FIELD_BUDGET_NAME_SAVE_ON_ORDER] = $budget->getName();
  72.                                 $data = [
  73.                                     [
  74.                                         'id' => $order->getId(),
  75.                                         'customFields' => $customFields
  76.                                     ]
  77.                                 ];
  78.                                 $this->orderRepository->upsert($data, $event->getContext());
  79.                             }
  80.                         }
  81.                     }
  82.                 }
  83.             }
  84.         }
  85.     }
  86.     private function getBudgetPaymentMethodId(): ?string
  87.     {
  88.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('name', 'budget'));
  89.         return $this->paymentMethodRepository->searchIds($paymentCriteria, Context::createDefaultContext())->firstId();
  90.     }
  91. }