custom/plugins/BjerregaardTheme/src/Subscriber/CartConvertedSubscriber.php line 43

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace BjerregaardTheme\Subscriber;
  3. use B2bOffer\Components\Offer\Converter\Events\OfferOrderConvertedEvent;
  4. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. class CartConvertedSubscriber implements EventSubscriberInterface
  10. {
  11.     private RequestStack $requestStack;
  12.     public function __construct(RequestStack $requestStack)
  13.     {
  14.         $this->requestStack $requestStack;
  15.     }
  16.     public static function getSubscribedEvents()
  17.     {
  18.         return [
  19.             CartConvertedEvent::class => 'orderWritten',
  20.             OfferOrderConvertedEvent::class => 'offerConverted'
  21.         ];
  22.     }
  23.     public function offerConverted(OfferOrderConvertedEvent $event): void
  24.     {
  25.         if ($event->getOffer()->getMatchCode())
  26.         {
  27.             $order $event->getConverted();
  28.             $order['customFields']['matchcode'] = $event->getOffer()->getMatchCode();
  29.             $event->setConverted($order);
  30.         }
  31.     }
  32.     public function orderWritten(CartConvertedEvent $event): void
  33.     {
  34.         $orderData $event->getConvertedCart();
  35.         $orderCustomFields $orderData['customFields'] ?? [];
  36.         $referenceField $this->requestStack->getCurrentRequest()->request->get('referenceField');
  37.         if ($referenceField) {
  38.             $orderCustomFields['tigermediareference'] = $referenceField;
  39.         }
  40.         $orderData['customFields'] = $orderCustomFields;
  41.         $event->setConvertedCart($orderData);
  42.     }
  43. }