custom/plugins/System4InterfacePlugin/src/Subscriber/OrderSubscriber.php line 69

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace System4InterfacePlugin\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  4. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  5. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionDefinition;
  6. use Shopware\Core\Checkout\Order\OrderEntity;
  7. use Shopware\Core\Checkout\Order\OrderStates;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use System4InterfacePlugin\Services\OrderService;
  14. use Shopware\Core\Framework\Context;
  15. use Shopware\Core\Defaults;
  16. class OrderSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var EntityRepository
  20.      */
  21.     private $orderRepository;
  22.     /**
  23.      * @var OrderService
  24.      */
  25.     private $orderService;
  26.     /**
  27.      * @var CartConvertedEvent
  28.      */
  29.     protected $cartConvertedEvent;
  30.     public function __construct(
  31.         EntityRepository $orderRepository,
  32.         OrderService $orderService
  33.     ) {
  34.         $this->orderRepository $orderRepository;
  35.         $this->orderService $orderService;
  36.     }
  37.     public static function getSubscribedEvents()
  38.     {
  39.         return [
  40.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
  41.             StateMachineTransitionEvent::class => 'onStateChanged'
  42.         ];
  43.     }
  44.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event)
  45.     {   
  46.         $order $event->getOrder();
  47.         $this->orderRepository->update([
  48.             [
  49.                 'id' => $order->getId(),
  50.                 'customFields' => ['system4_erp_status' => "Not Received"]
  51.             ]
  52.         ], Context::createDefaultContext());
  53.         $orderTransaction $order->getTransactions()->first();
  54.         if ($orderTransaction->getPaymentMethod() && $orderTransaction->getPaymentMethod()->isSynchronous()) {
  55.             $order $this->getOrder($event$order->getId());
  56.             $response $this->orderService->sendOrderPayload($order$order->getOrderCustomer()->getCustomer());
  57.         }
  58.     }
  59.     public function onStateChanged(StateMachineTransitionEvent $event)
  60.     {
  61.         if ($event->getEntityName() !== OrderTransactionDefinition::ENTITY_NAME) {
  62.             return;
  63.         }
  64.         $to $event->getToPlace()->getTechnicalName();
  65.         $from $event->getFromPlace()->getTechnicalName();
  66.         if ($from === OrderStates::STATE_OPEN && $to !== OrderStates::STATE_CANCELLED) {
  67.             $order $this->getOrderTransaction($event$event->getEntityId());
  68.             $this->orderRepository->update([
  69.                 [
  70.                     'id' => $order->getId(),
  71.                     'customFields' => ['system4_erp_status' => "Not Received"]
  72.                 ]
  73.             ], Context::createDefaultContext());
  74.             $orderTransaction $order->getTransactions()->first();
  75.             if ($orderTransaction->getPaymentMethod()->isAsynchronous()) {
  76.                 $response $this->orderService->sendOrderPayload($order$order->getOrderCustomer()->getCustomer());
  77.             }
  78.         }
  79.     }
  80.     private function getOrder(CheckoutOrderPlacedEvent $eventstring $id): ?OrderEntity
  81.     {
  82.         $criteria = new Criteria([$id]);
  83.         $criteria->addAssociation('orderCustomer.customer');
  84.         $criteria->addAssociation('lineItems');
  85.         $criteria->addAssociation('transactions.paymentMethod');
  86.         $criteria->addAssociation('billingAddress.country');
  87.         $criteria->addAssociation('deliveries.shippingMethod');
  88.         $criteria->addAssociation('deliveries.shippingOrderAddress.country');
  89.         $criteria->addAssociation('currency');
  90.         $criteria->addAssociation('language.translationCode');
  91.         return $this->orderRepository->search($criteria$event->getContext())->first();
  92.     }
  93.     private function getOrderTransaction(StateMachineTransitionEvent $eventstring $id): ?OrderEntity
  94.     {
  95.         $criteria = new Criteria();
  96.         $criteria->addFilter(new EqualsFilter('transactions.id'$id));
  97.         $criteria->addAssociation('orderCustomer.customer');
  98.         $criteria->addAssociation('lineItems');
  99.         $criteria->addAssociation('transactions.paymentMethod');
  100.         $criteria->addAssociation('billingAddress.country');
  101.         $criteria->addAssociation('deliveries.shippingMethod');
  102.         $criteria->addAssociation('deliveries.shippingOrderAddress.country');
  103.         return $this->orderRepository->search($criteria$event->getContext())->first();
  104.     }
  105. }