<?php declare(strict_types=1);
namespace Okeonline\MyKeje\Storefront\Subscriber;
use Doctrine\DBAL\Connection;
use Shopware\Core\Checkout\Order\OrderEvents;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class OrderLineItemCreatedEventSubscriber implements EventSubscriberInterface
{
/** @var EntityRepository $orderLineItemRepository */
private $orderLineItemRepository;
/** @var Connection $connection */
private $connection;
public function __construct(
EntityRepository $orderLineItemRepository,
Connection $connection
)
{
$this->orderLineItemRepository = $orderLineItemRepository;
$this->connection = $connection;
}
public static function getSubscribedEvents(): array
{
return [
OrderEvents::ORDER_LINE_ITEM_WRITTEN_EVENT => 'manipulateReferenceTable'
];
}
public function manipulateReferenceTable(EntityWrittenEvent $event)
{
$writeResults = $event->getWriteResults();
$entityName = $event->getEntityName();
if( ! $entityName == 'order_line_item' || ! $writeResults || ! is_array($writeResults) || count($writeResults) == 0)
return;
foreach ($writeResults as $entityWriteResult)
{
$orderLineItemId = $entityWriteResult->getPrimaryKey();
$criteria = new Criteria([$orderLineItemId]);
$criteria->addAssociation('order');
$criteria->addAssociation('order.order_customer');
$criteria->addAssociation('order.order_line_items');
$result = $this->orderLineItemRepository->search($criteria, $event->getContext());
if($result->count() !== 1)
continue;
$orderLineItem = $result->getEntities()->first();
$orderId = $orderLineItem->getOrder()->getId();
$customerId = $orderLineItem->getOrder()->getOrderCustomer()->getCustomerId();
$this->connection->executeStatement(sprintf("
DELETE FROM `oo_mykeje_reference` WHERE `customer_id` = UNHEX('%s') AND `order_id` = UNHEX('%s') AND `order_line_item_id` = UNHEX('%s');
", $customerId, $orderId, $orderLineItemId));
if(
! is_array($entityWriteResult->getPayload()) ||
! array_key_exists('type', $entityWriteResult->getPayload()) ||
! array_key_exists('label', $entityWriteResult->getPayload()) ||
! array_key_exists('payload', $entityWriteResult->getPayload()) ||
! $entityWriteResult->getPayload()['type'] == 'customized-products-option' ||
strtolower($entityWriteResult->getPayload()['label']) !== 'referentie' ||
! is_array($entityWriteResult->getPayload()['payload']) ||
! array_key_exists('value', $entityWriteResult->getPayload()['payload']) ||
is_null($entityWriteResult->getPayload()['payload']['value'])
)
continue;
$reference = $entityWriteResult->getPayload()['payload']['value'];
$this->connection->executeStatement(sprintf("
INSERT INTO `oo_mykeje_reference` (`customer_id`, `order_id`, `order_line_item_id`, `reference`) VALUES (UNHEX('%s'), UNHEX('%s'), UNHEX('%s'), '%s');
", $customerId, $orderId, $orderLineItemId, addslashes($reference)));
}
}
}