<?php
namespace EnetQrCode\Subscriber;
use EnetQrCode\Services\QrCodeService;
use Shopware\Core\Checkout\Order\OrderEvents;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
class OrderPlacedSubscriber implements EventSubscriberInterface
{
/** @var EntityRepository $orderRepository */
private $orderRepository;
public function __construct(EntityRepository $orderRepository)
{
$this->orderRepository = $orderRepository;
}
public static function getSubscribedEvents()
{
return [
OrderEvents::ORDER_WRITTEN_EVENT => 'onOrderPlaced',
];
}
public function onOrderPlaced(EntityWrittenEvent $event)
{
$context = $event->getContext();
foreach($event->getWriteResults() as $result) {
// if ($result->getExistence() !== null && $result->getExistence()->exists()) {
// break;
// }
$payload = $result->getPayload();
if(!isset($payload['id'])) {
return;
}
$orderResult = $this->orderRepository->search(
new Criteria([$payload['id']]),
$context
);
$elements = $orderResult->getElements();
if(count($elements) > 0) {
$element = $elements[array_keys($elements)[0]];
$qrCode = new QrCodeService();
$qrCode->setValue($element->getOrderNumber())->create();
}
}
}
}