custom/plugins/EnetQrCode/src/Subscriber/OrderPlacedSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. namespace EnetQrCode\Subscriber;
  3. use EnetQrCode\Services\QrCodeService;
  4. use Shopware\Core\Checkout\Order\OrderEvents;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  9. class OrderPlacedSubscriber implements EventSubscriberInterface
  10. {
  11.     /** @var EntityRepository $orderRepository */
  12.     private $orderRepository;
  13.     public function __construct(EntityRepository $orderRepository)
  14.     {
  15.         $this->orderRepository $orderRepository;
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return [
  20.             OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderPlaced',
  21.         ];
  22.     }
  23.     public function onOrderPlaced(EntityWrittenEvent $event)
  24.     {
  25.         $context $event->getContext();
  26.         foreach($event->getWriteResults() as $result) {
  27. //            if ($result->getExistence() !== null && $result->getExistence()->exists()) {
  28. //                break;
  29. //            }
  30.             $payload $result->getPayload();
  31.         
  32.             if(!isset($payload['id'])) {
  33.         return;
  34.         }
  35.             $orderResult $this->orderRepository->search(
  36.                 new Criteria([$payload['id']]),
  37.                 $context
  38.             );
  39.             $elements $orderResult->getElements();
  40.             if(count($elements) > 0) {
  41.                 $element $elements[array_keys($elements)[0]];
  42.                 $qrCode = new QrCodeService();
  43.                 $qrCode->setValue($element->getOrderNumber())->create();
  44.             }
  45.         }
  46.     }
  47. }