<?php declare(strict_types=1);
namespace BjerregaardTheme\Subscriber;
use B2bOffer\Components\Offer\Converter\Events\OfferOrderConvertedEvent;
use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class CartConvertedSubscriber implements EventSubscriberInterface
{
private RequestStack $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents()
{
return [
CartConvertedEvent::class => 'orderWritten',
OfferOrderConvertedEvent::class => 'offerConverted'
];
}
public function offerConverted(OfferOrderConvertedEvent $event): void
{
if ($event->getOffer()->getMatchCode())
{
$order = $event->getConverted();
$order['customFields']['matchcode'] = $event->getOffer()->getMatchCode();
$event->setConverted($order);
}
}
public function orderWritten(CartConvertedEvent $event): void
{
$orderData = $event->getConvertedCart();
$orderCustomFields = $orderData['customFields'] ?? [];
$referenceField = $this->requestStack->getCurrentRequest()->request->get('referenceField');
if ($referenceField) {
$orderCustomFields['tigermediareference'] = $referenceField;
}
$orderData['customFields'] = $orderCustomFields;
$event->setConvertedCart($orderData);
}
}