<?php declare(strict_types=1);
namespace FourtwosixWirthsExtension\Subscriber;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EnrichOrderPayload implements EventSubscriberInterface
{
private $lineItemRepository;
public function __construct(
EntityRepository $lineItemRepository
)
{
$this->lineItemRepository = $lineItemRepository;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
];
}
public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
{
$context = $event->getContext();
foreach ($event->getOrder()->getLineItems() as $item) {
if (!is_null($item->getParentId())) {
if ($item->getProductId()) {
$this->lineItemRepository->upsert([['id' => $item->getId(), 'identifier'=> $item->getProductId()]], $context);
}
}
}
}
}