custom/plugins/WbfkExtensions/src/Subscriber/RestoreDeliveryTimeDataInOrder.php line 59

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace WbfkExtensions\Subscriber;
  4. use DateTime;
  5. use DateTimeZone;
  6. use Exception;
  7. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  8. use Shopware\Core\Checkout\Order\OrderEntity;
  9. use Shopware\Core\Checkout\Order\OrderEvents;
  10. use Shopware\Core\Defaults;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use WbfkExtensions\Core\Checkout\Cart\Delivery\DeliveryMode;
  17. use WbfkExtensions\Core\Checkout\Cart\Delivery\ExpectedProductDeliveryTimeByQuantity\ExpectedProductDeliveryTimeByQuantityCollection;
  18. use WbfkExtensions\Core\Checkout\Cart\Delivery\ExpectedProductDeliveryTimeByQuantity\ExpectedProductDeliveryTimeByQuantityEntity;
  19. use WbfkExtensions\Core\Checkout\Cart\Delivery\PossibleDeliveryModes;
  20. class RestoreDeliveryTimeDataInOrder implements EventSubscriberInterface
  21. {
  22.     public function __construct(
  23.         private readonly EntityRepository $productRepository
  24.     )
  25.     {
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             OrderEvents::ORDER_LOADED_EVENT => 'restoreDeliveryModeForOrder',
  31.             OrderEvents::ORDER_LINE_ITEM_LOADED_EVENT => 'onOrderLineItemLoadedEvent',
  32.         ];
  33.     }
  34.     public function restoreDeliveryModeForOrder(EntityLoadedEvent $event): void
  35.     {
  36.         foreach ($event->getEntities() as $order) {
  37.             /** @var OrderEntity $order */
  38.             $deliveryModeData $order->getCustomFields()['deliveryMode'] ?? false;
  39.             if (!$deliveryModeData || !isset($deliveryModeData['selectedDeliveryMode'])) {
  40.                 continue;
  41.             }
  42.             $deliveryMode = new DeliveryMode(
  43.                 PossibleDeliveryModes::from($deliveryModeData['selectedDeliveryMode']),
  44.                 $deliveryModeData['cartRequiresPartialDelivery']
  45.             );
  46.             /** @noinspection PhpDeprecationInspection */
  47.             $order->addExtension(DeliveryMode::CART_EXTENSION_KEY$deliveryMode);
  48.         }
  49.     }
  50.     public function onOrderLineItemLoadedEvent(EntityLoadedEvent $event)
  51.     {
  52.         $this->assignLineItemCoverImage($event);
  53.     }
  54.     /**
  55.      * @throws Exception
  56.      */
  57.     private function assignLineItemCoverImage(EntityLoadedEvent $event): void
  58.     {
  59.         /** @var OrderLineItemEntity $lineItem */
  60.         foreach ($event->getEntities() as $lineItem) {
  61.             $productId $lineItem->getProductId();
  62.             if ($productId) {
  63.                 $productCriteria = new Criteria([$productId]);
  64.                 $productCriteria->addAssociation('cover');
  65.                 $productCriteria->setLimit(1);
  66.                 $product $this->productRepository->search($productCriteria$event->getContext())->first();
  67.                 if ($product) {
  68.                     $lineItem->assign([
  69.                         'coverImage' => $product->getCover(),
  70.                     ]);
  71.                 }
  72.             }
  73.         }
  74.     }
  75. }