custom/plugins/NieMehrStreichenTheme/src/Service/ListenToOrderChanges.php line 38

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace NieMehrStreichenTheme\Service;
  3. use Shopware\Core\Checkout\Order\OrderEvents;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use GuzzleHttp\Client;
  9. class ListenToOrderChanges implements EventSubscriberInterface
  10. {
  11.     private $restClient;
  12.     
  13.     private EntityRepository $repository;
  14.     
  15.     private OrderTransactionRepository $OrderTransactionRepository;
  16.     public function __construct(
  17.         EntityRepository $repository,
  18.         EntityRepository $orderTransactionRepository
  19.     ) {
  20.         $this->repository $repository;
  21.         $this->orderTransactionRepository $orderTransactionRepository;
  22.         $this->restClient = new Client();
  23.     }
  24.     
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             OrderEvents::ORDER_WRITTEN_EVENT => 'notifyNewOrderToContactPro',
  29.             OrderEvents::ORDER_TRANSACTION_WRITTEN_EVENT => 'notifyPayedOrderToContactPro',
  30.         ];
  31.     }
  32.     public function notifyPayedOrderToContactPro(EntityWrittenEvent $event): void
  33.     {
  34.         $criteria = new Criteria([$event->getIds()[0]]);
  35.         $order $this->orderTransactionRepository->search($criteria$event->getContext())->first();
  36.         if($order->getStateId() == "06be24d74fe14faeaa7de5f153fdd847"){ // paid
  37.             $this->sendNotifyToContactPro($event$order->getOrderId());
  38.         }
  39.     }
  40.     public function notifyNewOrderToContactPro(EntityWrittenEvent $event): void
  41.     {
  42.         foreach ($event->getWriteResults() as $writeResult) {
  43.             $orderID $writeResult->getPrimaryKey();
  44.             
  45.             $this->sendNotifyToContactPro($event$orderID);
  46.         }
  47.     }
  48.     
  49.     public function sendNotifyToContactPro($event$orderID) {
  50.         
  51.         $criteria = new Criteria([$orderID]);
  52.         $criteria->addAssociation('transactions');
  53.         $criteria->addAssociation('orderCustomer');
  54.         $criteria->addAssociation('lineItems');
  55.         $order $this->repository->search($criteria$event->getContext())->first();
  56.         
  57.         $request = new \GuzzleHttp\Psr7\Request(
  58.             'POST',
  59.             'https://objednavky-transform.animatec.cz/transform/shopware/niemehr-order',
  60.             [
  61.                 'Content-Type' => 'application/json',
  62.                 'Authorization' => 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJrMDRzajNnOXU4ZzR2Nm10bWZsNWJuMXYyNyIsImlhdCI6MTY4NTQzNTU0NSwiZXhwIjpudWxsfQ.jp9ZTZRotWIeSBagO8zBWK7NbegM9yPYYbJuh0Y5mW4',
  63.             ],
  64.             json_encode([
  65.                 'external_id' => $order->orderNumber,
  66.             ])
  67.         );
  68.         $this->restClient->send($request);
  69.     }
  70. }