custom/plugins/WbfkOffer/src/Subscriber/AddOrderDetailsToOfferAndMarkAsOrdered.php line 35

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wbfk\Offer\Subscriber;
  4. use Shopware\Core\Checkout\Order\OrderEntity;
  5. use Shopware\Core\Checkout\Order\OrderEvents;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Wbfk\Offer\Core\Entity\Offer\OfferCollection;
  12. use Wbfk\Offer\Core\Entity\Offer\OfferEntity;
  13. use Wbfk\Offer\Core\Offer\Storefront\OfferService;
  14. use Wbfk\Offer\Service\OfferLineItemHandler;
  15. class AddOrderDetailsToOfferAndMarkAsOrdered implements EventSubscriberInterface
  16. {
  17.     public function __construct(
  18.         private readonly OfferService $offerService,
  19.         private readonly EntityRepository $orderRepository,
  20.     ) {
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             OrderEvents::ORDER_WRITTEN_EVENT => 'addOrderDetailsToOfferAndMarkAsOrdered',
  26.         ];
  27.     }
  28.     public function addOrderDetailsToOfferAndMarkAsOrdered(EntityWrittenEvent $event): void
  29.     {
  30.         foreach ($event->getWriteResults() as $result) {
  31.             $orderId $result->getPayload()['id'] ?? null;
  32.             if (!$orderId) {
  33.                 continue;
  34.             }
  35.             $criteria = new Criteria([$orderId]);
  36.             $criteria->addAssociation('lineItems');
  37.             /** @var ?OrderEntity $order */
  38.             $order $this->orderRepository->search($criteria$event->getContext())->first();
  39.             if (!$order) {
  40.                 return;
  41.             }
  42.             $offerLineItems $order->getLineItems()->filterByType(OfferLineItemHandler::TYPE_OFFER);
  43.             if ($offerLineItems->count() === 0) {
  44.                 return;
  45.             }
  46.             $offerIds $offerLineItems->fmap(function ($offerLineItem) {
  47.                 return $offerLineItem->getReferencedId();
  48.             });
  49.             /** @var OfferCollection|OfferEntity[] $offers */
  50.             $offers $this->offerService->getOffersByIds($offerIds$event->getContext());
  51.             foreach ($offers as $offer) {
  52.                 if ($offer->isRunning() || $offer->isOrdered()) {
  53.                     // Do not update offer with order details if offer is running or already ordered
  54.                     // When an existing order is edited and saved again, this event is triggered again,
  55.                     // so already ordered offers would be updated again.
  56.                     continue;
  57.                 }
  58.                 $offerContext $this->offerService->createScContextForOffer($offer);
  59.                 $this->offerService->addOrderDetailsAndMarkAsOrdered($offer$order->getId(), $order->getVersionId(), $offerContext);
  60.             }
  61.         }
  62.     }
  63. }