custom/plugins/EwGrecoBundle/src/Subscriber/OrderSubscriber.php line 33

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Ew\GrecoBundle\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  4. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\Uuid\Uuid;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class OrderSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var EntityRepository
  12.      */
  13.     private $customerDnaProductRepository;
  14.     public function __construct(
  15.         EntityRepository $customerDnaProductRepository
  16.     )
  17.     {
  18.         $this->customerDnaProductRepository $customerDnaProductRepository;
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced'
  24.         ];
  25.     }
  26.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
  27.     {
  28.         try {
  29.             $order $event->getOrder();
  30.             $customerId $order->getOrderCustomer()->getCustomerId();
  31.             $lineItems $order->getLineItems();
  32.             /** @var OrderLineItemEntity $lineItem */
  33.             foreach ($lineItems as $lineItem) {
  34.                 $payload $lineItem->getPayload();
  35.                 $customFields = [];
  36.                 if (isset($payload['customFields'])) {
  37.                     $customFields $payload['customFields'];
  38.                 }
  39.                 if ($customFields) {
  40.                     if (isset($customFields['greco_dna_article']) && $customFields['greco_dna_article'] === true) {
  41.                         $productId $lineItem->getReferencedId();
  42.                         if ($productId && $customerId) {
  43.                             $this->customerDnaProductRepository->upsert(
  44.                                 [['id' => Uuid::randomHex(), 'customerId' => $customerId'productId' => $productId]], $event->getContext()
  45.                             );
  46.                         }
  47.                     }
  48.                 }
  49.             }
  50.         } catch (\Exception $exception) {
  51.         }
  52.     }
  53. }