custom/plugins/MyKeje/src/Storefront/Subscriber/OrderLineItemCreatedEventSubscriber.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 OrderLineItemCreatedEventSubscriber implements EventSubscriberInterface
  10. {
  11.     /** @var EntityRepository $orderLineItemRepository */
  12.     private $orderLineItemRepository;
  13.     /** @var Connection $connection */
  14.     private $connection;
  15.     public function __construct(
  16.         EntityRepository $orderLineItemRepository,
  17.         Connection $connection
  18.     )
  19.     {
  20.         $this->orderLineItemRepository $orderLineItemRepository;
  21.         $this->connection $connection;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             OrderEvents::ORDER_LINE_ITEM_WRITTEN_EVENT => 'manipulateReferenceTable'
  27.         ];
  28.     }
  29.     public function manipulateReferenceTable(EntityWrittenEvent $event)
  30.     {
  31.         $writeResults $event->getWriteResults();
  32.         $entityName $event->getEntityName();
  33.         if( ! $entityName == 'order_line_item' || ! $writeResults || ! is_array($writeResults) || count($writeResults) == 0)
  34.             return;
  35.         foreach ($writeResults as $entityWriteResult)
  36.         {
  37.             $orderLineItemId $entityWriteResult->getPrimaryKey();
  38.             $criteria = new Criteria([$orderLineItemId]);
  39.             $criteria->addAssociation('order');
  40.             $criteria->addAssociation('order.order_customer');
  41.             $criteria->addAssociation('order.order_line_items');
  42.             $result $this->orderLineItemRepository->search($criteria$event->getContext());
  43.             if($result->count() !== 1)
  44.                 continue;
  45.             $orderLineItem $result->getEntities()->first();
  46.             $orderId $orderLineItem->getOrder()->getId();
  47.             $customerId $orderLineItem->getOrder()->getOrderCustomer()->getCustomerId();
  48.             $this->connection->executeStatement(sprintf("
  49.                 DELETE FROM `oo_mykeje_reference` WHERE `customer_id` = UNHEX('%s') AND `order_id` = UNHEX('%s') AND `order_line_item_id` = UNHEX('%s');
  50.             "$customerId$orderId$orderLineItemId));
  51.             if(
  52.                 ! is_array($entityWriteResult->getPayload()) ||
  53.                 ! array_key_exists('type'$entityWriteResult->getPayload()) ||
  54.                 ! array_key_exists('label'$entityWriteResult->getPayload()) ||
  55.                 ! array_key_exists('payload'$entityWriteResult->getPayload()) ||
  56.                 ! $entityWriteResult->getPayload()['type'] == 'customized-products-option' ||
  57.                   strtolower($entityWriteResult->getPayload()['label']) !== 'referentie' ||
  58.                 ! is_array($entityWriteResult->getPayload()['payload']) ||
  59.                 ! array_key_exists('value'$entityWriteResult->getPayload()['payload']) ||
  60.                 is_null($entityWriteResult->getPayload()['payload']['value'])
  61.             )
  62.                 continue;
  63.             $reference $entityWriteResult->getPayload()['payload']['value'];
  64.             $this->connection->executeStatement(sprintf("
  65.                 INSERT INTO `oo_mykeje_reference` (`customer_id`, `order_id`, `order_line_item_id`, `reference`) VALUES (UNHEX('%s'), UNHEX('%s'), UNHEX('%s'), '%s');
  66.             "$customerId$orderId$orderLineItemIdaddslashes($reference)));
  67.         }
  68.     }
  69. }