custom/plugins/MyKeje/src/Storefront/Subscriber/OrderCreatedEventSubscriber.php line 37

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Okeonline\MyKeje\Storefront\Subscriber;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Checkout\Order\OrderEvents;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class OrderCreatedEventSubscriber implements EventSubscriberInterface
  10. {
  11.     /** @var EntityRepository $orderRepository */
  12.     private $orderRepository;
  13.     /** @var Connection $connection */
  14.     private $connection;
  15.     public function __construct(
  16.         EntityRepository $orderRepository,
  17.         Connection $connection
  18.     )
  19.     {
  20.         $this->orderRepository $orderRepository;
  21.         $this->connection $connection;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             OrderEvents::ORDER_WRITTEN_EVENT => 'manipulateReferenceTable'
  27.         ];
  28.     }
  29.     public function manipulateReferenceTable(EntityWrittenEvent $event)
  30.     {
  31.         $writeResults $event->getWriteResults();
  32.         $entityName $event->getEntityName();
  33.         if( ! $entityName == 'order' || ! $writeResults || ! is_array($writeResults) || count($writeResults) == 0)
  34.             return;
  35.         foreach ($writeResults as $entityWriteResult)
  36.         {
  37.             $orderId $entityWriteResult->getPrimaryKey();
  38.             $criteria = new Criteria([$orderId]);
  39.             $criteria->addAssociation('order_customer');
  40.             $result $this->orderRepository->search($criteria$event->getContext());
  41.             if($result->count() !== 1)
  42.                 continue;
  43.             $order $result->getEntities()->first();
  44.             $customerId $order->getOrderCustomer()->getCustomerId();
  45.             $this->connection->executeStatement(sprintf("
  46.                 DELETE FROM `oo_mykeje_reference` WHERE `customer_id` = UNHEX('%s') AND `order_id` = UNHEX('%s') AND `order_line_item_id` = NULL;
  47.             "$customerId$orderId));
  48.             if(is_array($order->getCustomFields()) && array_key_exists('oo_checkout_extra_data_reference'$order->getCustomFields()) && !is_null($order->getCustomFields()['oo_checkout_extra_data_reference']))
  49.             {
  50.                 $reference addslashes($order->getCustomFields()['oo_checkout_extra_data_reference']);
  51.                 $this->connection->executeStatement(sprintf("
  52.                     INSERT INTO `oo_mykeje_reference` (`customer_id`, `order_id`, `order_line_item_id`, `reference`) VALUES (UNHEX('%s'), UNHEX('%s'), NULL, '%s');
  53.                 "$customerId$orderId$reference));
  54.             }
  55.         }
  56.     }
  57. }