custom/plugins/MasterFFLCheckout/src/Subscriber/OrderSubscriber.php line 31

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MasterFFL\Checkout\Subscriber;
  3. use Shopware\Core\Checkout\Order\OrderEvents;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Psr\Log\LoggerInterface;
  8. class OrderSubscriber implements EventSubscriberInterface
  9. {
  10.     private EntityRepositoryInterface $orderRepository;
  11.     private LoggerInterface $logger;
  12.     public function __construct(
  13.         EntityRepositoryInterface $orderRepository,
  14.         LoggerInterface $logger
  15.     ) {
  16.         $this->orderRepository $orderRepository;
  17.         $this->logger $logger;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderWritten'
  23.         ];
  24.     }
  25.     public function onOrderWritten(EntityWrittenEvent $event): void
  26.     {
  27.         foreach ($event->getWriteResults() as $writeResult) {
  28.             $payload $writeResult->getPayload();
  29.             
  30.             // Check if this is an FFL order
  31.             if (isset($payload['customFields']['is_ffl_order']) && $payload['customFields']['is_ffl_order']) {
  32.                 $this->handleFFLOrder($payload$event->getContext());
  33.             }
  34.         }
  35.     }
  36.     private function handleFFLOrder(array $orderData$context): void
  37.     {
  38.         try {
  39.             $this->logger->info('Processing FFL order', [
  40.                 'orderId' => $orderData['id'],
  41.                 'fflDealerId' => $orderData['customFields']['ffl_dealer_id'] ?? null
  42.             ]);
  43.             // Store FFL dealer information in order custom fields
  44.             if (isset($orderData['customFields']['ffl_dealer_id'])) {
  45.                 $this->updateOrderWithFFLData($orderData['id'], $orderData['customFields'], $context);
  46.             }
  47.         } catch (\Exception $e) {
  48.             $this->logger->error('Error processing FFL order', [
  49.                 'orderId' => $orderData['id'],
  50.                 'error' => $e->getMessage()
  51.             ]);
  52.         }
  53.     }
  54.     private function updateOrderWithFFLData(string $orderId, array $customFields$context): void
  55.     {
  56.         $this->orderRepository->update([
  57.             [
  58.                 'id' => $orderId,
  59.                 'customFields' => array_merge($customFields, [
  60.                     'ffl_processed' => true,
  61.                     'ffl_processing_date' => date('Y-m-d H:i:s')
  62.                 ])
  63.             ]
  64.         ], $context);
  65.     }
  66. }