custom/plugins/DkcPentagastOrderExport/src/Subscriber/OrderCreatedSubscriber.php line 45

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DkcPentagastOrderExport\Subscriber;
  3. use DkcPentagastOrderExport\Service\PentagastOrderExportService;
  4. use Shopware\Core\Checkout\Order\OrderEntity;
  5. use Shopware\Core\Checkout\Order\OrderEvents;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class OrderCreatedSubscriber implements EventSubscriberInterface
  14. {
  15.     private PentagastOrderExportService $exportService;
  16.     private EntityRepository $orderRepository;
  17.     /** @var mixed */
  18.     private $salesChannelRepository;
  19.     private SystemConfigService $systemConfigService;
  20.     public function __construct(
  21.         PentagastOrderExportService $exportService,
  22.         EntityRepository $orderRepository,
  23.         $salesChannelRepository,
  24.         SystemConfigService $systemConfigService
  25.     ) {
  26.         $this->exportService $exportService;
  27.         $this->orderRepository $orderRepository;
  28.         $this->salesChannelRepository $salesChannelRepository;
  29.         $this->systemConfigService $systemConfigService;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderWritten',
  35.         ];
  36.     }
  37.     public function onOrderWritten(EntityWrittenEvent $event): void
  38.     {
  39.         $context $event->getContext();
  40.         $ids $event->getIds();
  41.         if (empty($ids)) {
  42.             return;
  43.         }
  44.         $criteria = (new Criteria($ids))
  45.             ->addAssociation('salesChannel')
  46.             ->addAssociation('lineItems')
  47.             ->addAssociation('deliveries')
  48.             ->addAssociation('deliveries.shippingOrderAddress.country')
  49.             ->addAssociation('transactions.paymentMethod')
  50.             ->addAssociation('billingAddress.country')
  51.             ->addAssociation('orderCustomer');
  52.         $orders $this->orderRepository->search($criteria$context);
  53.         /** @var OrderEntity $order */
  54.         foreach ($orders as $order) {
  55.             /** @var SalesChannelEntity|null $salesChannel */
  56.             $salesChannel $order->getSalesChannel();
  57.             if (!$salesChannel) {
  58.                 $salesChannelId $order->getSalesChannelId();
  59.                 $criteria = new Criteria([$salesChannelId]);
  60.                 $salesChannel $this->salesChannelRepository->search($criteria$context)->first();
  61.                 if (!$salesChannel) {
  62.                     continue;
  63.                 }
  64.             }
  65.             $salesChannelId $salesChannel->getId();
  66.             $triggerState = (string) ($this->systemConfigService->get(
  67.                 'DkcPentagastOrderExport.config.triggerState',
  68.                 $salesChannelId
  69.             ) ?? 'transaction_paid');
  70.             if ($triggerState !== 'all_orders') {
  71.                 continue;
  72.             }
  73.             $this->exportService->exportOrder($order$salesChannel);
  74.         }
  75.     }
  76. }