custom/plugins/KilbShopwareProductDesigner/src/Subscriber/OrderSubscriber.php line 49

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Kilb\KilbShopwareProductDesigner\Subscriber;
  3. use Kilb\KilbShopwareProductDesigner\Constants;
  4. use Kilb\KilbShopwareProductDesigner\Services\DesignGroupServiceInterface;
  5. use Kilb\KilbShopwareProductDesigner\Services\ProductServiceInterface;
  6. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class OrderSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var DesignGroupServiceInterface
  12.      */
  13.     private $designGroupService;
  14.     /**
  15.      * @var ProductServiceInterface
  16.      */
  17.     private $productService;
  18.     /**
  19.      * @param DesignGroupServiceInterface $designGroupService
  20.      * @param ProductServiceInterface $productService
  21.      */
  22.     public function __construct(
  23.         DesignGroupServiceInterface $designGroupService,
  24.         ProductServiceInterface $productService
  25.     ) {
  26.         $this->designGroupService $designGroupService;
  27.         $this->productService $productService;
  28.     }
  29.     /**
  30.      * @return string[]
  31.      */
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return[
  35.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
  36.         ];
  37.     }
  38.     /**
  39.      * @param CheckoutOrderPlacedEvent $event
  40.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  41.      */
  42.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
  43.     {
  44.         $order $event->getOrder();
  45.         $context $event->getContext();
  46.         foreach ($order->getLineItems() ?: [] as $lineItem) {
  47.             $payload $lineItem->getPayload();
  48.             if (true === empty($payload[Constants::LINE_ITEM_PAYLOAD_KEY_PRODUCT_NUMBER])) {
  49.                 continue;
  50.             }
  51.             $product $this->productService->getProductByProductNumber(
  52.                 $payload[Constants::LINE_ITEM_PAYLOAD_KEY_PRODUCT_NUMBER],
  53.                 $context
  54.             );
  55.             if (null === $product) {
  56.                 continue;
  57.             }
  58.             if (!empty($lineItem->getPayload()[Constants::LINE_ITEM_PAYLOAD_KEY_GROUP_SECRET])) {
  59.                 $secret $lineItem->getPayload()[Constants::LINE_ITEM_PAYLOAD_KEY_GROUP_SECRET];
  60.                 $data = [
  61.                     'orderId' => $order->getId(),
  62.                     'productId' => $product->getId(),
  63.                     'orderLineItemId' => $lineItem->getId(),
  64.                     'orderedAt' => date('Y-m-d H:i:s'),
  65.                 ];
  66.                 $this->designGroupService->updateDesignGroupBySecret(
  67.                     $secret,
  68.                     $data,
  69.                     $context
  70.                 );
  71.             }
  72.         }
  73.     }
  74. }