custom/plugins/IesProductNote/src/Subscriber/CheckoutConfirm.php line 59

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Ies\ProductNote\Subscriber;
  3. use Ies\ProductNote\Services\MediaUpdater;
  4. use Ies\ProductNote\Services\OrderUpdater;
  5. use Ies\ProductNote\Services\ProductNoteService;
  6. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  7. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  8. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  9. use Shopware\Core\Content\Media\MediaEntity;
  10. use Shopware\Core\Framework\Context;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
  13. /**
  14.  * @author    Lei Wang <lei.wang@isento-ecommcerce.de>
  15.  * @copyright 2022 isento eCommerce solutions GmbH
  16.  */
  17. class CheckoutConfirm implements EventSubscriberInterface
  18. {
  19.     private ProductNoteService $productNoteService;
  20.     private OrderUpdater $orderUpdater;
  21.     private MediaUpdater $mediaUpdater;
  22.     public function __construct(
  23.         ProductNoteService $productNoteService,
  24.         OrderUpdater $orderUpdater,
  25.         MediaUpdater $mediaUpdater
  26.     ) {
  27.         $this->productNoteService $productNoteService;
  28.         $this->orderUpdater $orderUpdater;
  29.         $this->mediaUpdater $mediaUpdater;
  30.     }
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             CartConvertedEvent::class => 'onCartConverted',
  35.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
  36.         ];
  37.     }
  38.     public function onCartConverted(CartConvertedEvent $event)
  39.     {
  40.         $convertedCart $event->getConvertedCart();
  41.         $salesChannelContext $event->getSalesChannelContext();
  42.         $convertedCart $this->addProductNoteToConvertedCart(
  43.             $convertedCart,
  44.             $event->getCart()->getLineItems(),
  45.             $salesChannelContext->getContext()
  46.         );
  47.         $event->setConvertedCart($convertedCart);
  48.     }
  49.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
  50.     {
  51.         $context $event->getContext();
  52.         $order $this->orderUpdater->updateCustomField($event->getOrderId(), $context);
  53.         $this->mediaUpdater->updateCustomField($order$context);
  54.         $this->productNoteService->removeAll();
  55.     }
  56.     private function addProductNoteToConvertedCart(
  57.         array $convertedCart,
  58.         LineItemCollection $cartLineItems,
  59.         Context $context
  60.     ): array {
  61.         foreach ($convertedCart['lineItems'] as $index => $convertedLineItem) {
  62.             $cartLineItem $cartLineItems->get($convertedLineItem['identifier']);
  63.             if ($productNote $this->productNoteService->getProductNote($cartLineItem->getId(), $context)) {
  64.                 $convertedCart['lineItems'][$index]['customFields']['ies_product_note'] = $productNote;
  65.             }
  66.         }
  67.         return $convertedCart;
  68.     }
  69. }