custom/plugins/RhiemRentalProducts/src/Subscriber/FrontendCheckout.php line 62

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Rhiem\RhiemRentalProducts\Subscriber;
  4. use Rhiem\RhiemRentalProducts\Components\RentalTime\RentalTime;
  5. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  6. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class FrontendCheckout implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var EntityRepositoryInterface
  13.      */
  14.     private $orderLineItemRepository;
  15.     public function __construct(EntityRepositoryInterface $orderLineItemRepo)
  16.     {
  17.         $this->orderLineItemRepository $orderLineItemRepo;
  18.     }
  19.     /**
  20.      * {@inheritDoc}
  21.      */
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
  26.             CartConvertedEvent::class => 'onConvertToOrder',
  27.         ];
  28.     }
  29.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
  30.     {
  31.         $orderLineItems $event->getOrder()->getLineItems()->getElements();
  32.         foreach ($orderLineItems as $orderLineItem) {
  33.             if (isset($orderLineItem->getPayload()['rentalProduct'])) {
  34.                 $rentalProductPayload $orderLineItem->getPayload()['rentalProduct'];
  35.                 /** @var RentalTime $rentalTime */
  36.                 $rentalTime $rentalProductPayload['rentalTime'];
  37.                 $this->orderLineItemRepository->update(
  38.                     [
  39.                         [
  40.                             'id' => $orderLineItem->getId(),
  41.                             'productId' => $orderLineItem->getReferencedId(),
  42.                             'customFields' => [
  43.                                 'rhiem_rental_products_rent_start' => $rentalTime->getStartDate(),
  44.                                 'rhiem_rental_products_rent_end' => $rentalTime->getEndDate(),
  45.                             ],
  46.                         ],
  47.                     ],
  48.                     $event->getContext()
  49.                 );
  50.             }
  51.         }
  52.     }
  53.     public function onConvertToOrder(CartConvertedEvent $event): void
  54.     {
  55.         $convertedCart $event->getConvertedCart();
  56.         $lineItems $convertedCart['lineItems'];
  57.         $orderHasRentalProduct false;
  58.         foreach ($lineItems as &$lineItem) {
  59.             if (!empty($lineItem['payload']['rentalProduct'])) {
  60.                 $orderHasRentalProduct true;
  61.                 $lineItem['payload']['rentalProduct']['rentalTime'] = serialize(
  62.                     $lineItem['payload']['rentalProduct']['rentalTime']
  63.                 );
  64.             }
  65.         }
  66.         if ($orderHasRentalProduct) {
  67.             $convertedCart['lineItems'] = $lineItems;
  68.             $event->setConvertedCart($convertedCart);
  69.         }
  70.     }
  71. }