custom/plugins/NetiNextEasyCoupon/src/Subscriber/OrderSubscriber.php line 134

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (c) 2020, Net Inventors GmbH
  4.  * @category  Shopware
  5.  * @author    mpeters
  6.  */
  7. declare(strict_types=1);
  8. namespace NetInventors\NetiNextEasyCoupon\Subscriber;
  9. use NetInventors\NetiNextEasyCoupon\Service\OrderVoucherService;
  10. use NetInventors\NetiNextEasyCoupon\Service\PluginConfig;
  11. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  12. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection;
  13. use Shopware\Core\Defaults;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  15. use Shopware\Core\System\SalesChannel\Event\SalesChannelContextSwitchEvent;
  16. use Shopware\Core\System\StateMachine\Event\StateMachineStateChangeEvent;
  17. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class OrderSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var PluginConfig
  23.      */
  24.     protected $pluginConfig;
  25.     /**
  26.      * @var OrderVoucherService
  27.      */
  28.     protected $orderVoucherService;
  29.     public function __construct(
  30.         PluginConfig $pluginConfig,
  31.         OrderVoucherService $orderVoucherService
  32.     ) {
  33.         $this->pluginConfig        $pluginConfig;
  34.         $this->orderVoucherService $orderVoucherService;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             CheckoutOrderPlacedEvent::class                 => [
  40.                 [ 'createVoucherByLineItems'],
  41.                 [ 'createTransactions'5000 ],
  42.             ],
  43.             'state_machine.order_transaction.state_changed' => [
  44.                 'sendVoucherActivateMailByOrderTransaction',
  45.             ],
  46.             // @TODO Also remove redemption voucher on context switch
  47.             SalesChannelContextSwitchEvent::class           => [
  48.                 'removePurchaseVoucherFromCart',
  49.             ],
  50.             CheckoutFinishPageLoadedEvent::class            => [
  51.                 ['displayPurchaseVouchers'],
  52.                 ['displayVoucherRestValue'5000 ],
  53.                 ['removeVoucherCodesFromSession'9000 ],
  54.             ],
  55.             'order_line_item.written' => 'onOrderLineItemWritten'
  56.         ];
  57.     }
  58.     public function onOrderLineItemWritten(EntityWrittenEvent $event): void
  59.     {
  60.         // if the live version of the order_line_item is updated, the associated transaction should be updated, too.
  61.         foreach ($event->getWriteResults() as $writeResult) {
  62.             $existence $writeResult->getExistence();
  63.             if (null === $existence || !$existence->exists()) {
  64.                 continue; // we don't care about deleted items here
  65.             }
  66.             $payload $writeResult->getPayload();
  67.             if ($payload['versionId'] !== Defaults::LIVE_VERSION) {
  68.                 continue; // we only update the transaction if the live version order_line_item is updated
  69.             }
  70.             $this->orderVoucherService->updateTransactionValueFromOrderLineItem(
  71.                 $writeResult->getPrimaryKey(),
  72.                 $event->getContext()
  73.             );
  74.         }
  75.     }
  76.     public function createVoucherByLineItems(CheckoutOrderPlacedEvent $event): void
  77.     {
  78.         if (!$this->pluginConfig->isActive()) {
  79.             return;
  80.         }
  81.         if (!$event->getOrder()->getLineItems() instanceof OrderLineItemCollection) {
  82.             return;
  83.         }
  84.         $this->orderVoucherService->createVoucherByLineItems($event->getOrder()->getLineItems(), $event->getContext());
  85.     }
  86.     public function displayPurchaseVouchers(CheckoutFinishPageLoadedEvent $event): void
  87.     {
  88.         if (!$this->pluginConfig->isActive()) {
  89.             return;
  90.         }
  91.         $this->orderVoucherService->addPurchaseVouchersToCheckoutConfirmPage($event->getPage(), $event->getContext());
  92.     }
  93.     public function displayVoucherRestValue(CheckoutFinishPageLoadedEvent $event): void
  94.     {
  95.         if (!$this->pluginConfig->isActive()) {
  96.             return;
  97.         }
  98.         $this->orderVoucherService->displayVoucherRestValueAtCheckoutConfirmPage($event->getPage(), $event->getContext());
  99.     }
  100.     public function createTransactions(CheckoutOrderPlacedEvent $event): void
  101.     {
  102.         if (!$this->pluginConfig->isActive()) {
  103.             return;
  104.         }
  105.         $this->orderVoucherService->createTransactionsForRedeemedVouchers($event->getOrder(), $event->getContext());
  106.     }
  107.     public function sendVoucherActivateMailByOrderTransaction(StateMachineStateChangeEvent $event): void
  108.     {
  109.         if (!$this->pluginConfig->isActive()) {
  110.             return;
  111.         }
  112.         $this->orderVoucherService->sendVoucherActivateMailByOrderTransaction(
  113.             $event->getTransition()->getEntityId(),
  114.             $event->getNextState()->getId(),
  115.             $event->getContext()
  116.         );
  117.     }
  118.     public function removePurchaseVoucherFromCart(SalesChannelContextSwitchEvent $event): void
  119.     {
  120.         if (!$this->pluginConfig->isActive()) {
  121.             return;
  122.         }
  123.         $currencyId $event->getRequestDataBag()->get('currencyId');
  124.         if (!(\is_string($currencyId) && '' !== $currencyId)) {
  125.             return;
  126.         }
  127.         $this->orderVoucherService->removePurchaseVoucherFromCart($event->getSalesChannelContext());
  128.     }
  129.     public function removeVoucherCodesFromSession(CheckoutFinishPageLoadedEvent $event): void
  130.     {
  131.         if (!$this->pluginConfig->isActive()) {
  132.             return;
  133.         }
  134.         $event->getRequest()->getSession()->remove('EasyCouponVoucherCodes');
  135.     }
  136. }