custom/plugins/AppflixStudygood/src/Storefront/Subscriber/CheckoutSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. namespace Appflix\Studygood\Storefront\Subscriber;
  3. use Appflix\Studygood\Core\Service\VideoCourseService;
  4. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  5. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates;
  6. use Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent;
  7. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  8. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class CheckoutSubscriber implements EventSubscriberInterface
  11. {
  12.     private VideoCourseService $videoCourseService;
  13.     public function __construct(
  14.         VideoCourseService $videoCourseService
  15.     )
  16.     {
  17.         $this->videoCourseService $videoCourseService;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             CheckoutConfirmPageLoadedEvent::class => 'onCartPageLoaded',
  23.             CheckoutOrderPlacedEvent::class => 'onOrderPlaced',
  24.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPage',
  25.             StateMachineTransitionEvent::class => 'stateChanged',
  26.         ];
  27.     }
  28.     public function onOrderPlaced(CheckoutOrderPlacedEvent $event): void
  29.     {
  30.         $this->videoCourseService->addEsdToCustomer($event->getOrder());
  31.     }
  32.     public function stateChanged(StateMachineTransitionEvent $event): void
  33.     {
  34.         if ($event->getContext()->getVersionId() !== \Shopware\Core\Defaults::LIVE_VERSION) {
  35.             return;
  36.         }
  37.         if ($event->getEntityName() === 'order_transaction' && $event->getToPlace()->getTechnicalName() === OrderTransactionStates::STATE_PAID) {
  38.             $order $this->videoCourseService->getOrderByTransaction($event->getEntityId());
  39.             $this->videoCourseService->updateOrderEsd($order);
  40.         }
  41.     }
  42.     public function onCartPageLoaded(CheckoutConfirmPageLoadedEvent $event): void
  43.     {
  44.         $cart $event->getPage()->getCart();
  45.         $cart->assign(['hasEsd' => $this->videoCourseService->cartHasEsd($cart)]);
  46.     }
  47.     public function onCheckoutFinishPage(CheckoutFinishPageLoadedEvent $event): void
  48.     {
  49.         $order $event->getPage()->getOrder();
  50.         $order->assign(['hasEsd' => $this->videoCourseService->orderHasEsd($order)]);
  51.     }
  52. }