custom/plugins/PremsIndividualOffer6/src/Subscriber/OrderConvertedSubscriber.php line 93

Open in your IDE?
  1. <?php
  2. /**
  3.  * PremSoft
  4.  * Copyright © 2021 Premsoft - Sven Mittreiter
  5.  *
  6.  * @copyright  Copyright (c) 2021, premsoft - Sven Mittreiter (http://www.premsoft.de)
  7.  * @author     Sven Mittreiter <info@premsoft.de>
  8.  */
  9. namespace Prems\Plugin\PremsIndividualOffer6\Subscriber;
  10. use Prems\Plugin\PremsIndividualOffer6\Core\Offer\Storefront\ConfigService;
  11. use Prems\Plugin\PremsIndividualOffer6\Core\Offer\Storefront\OfferService;
  12. use Prems\Plugin\PremsIndividualOffer6\PremsIndividualOffer6;
  13. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  14. use Shopware\Core\Checkout\Cart\Order\CartConvertedEvent;
  15. use Shopware\Core\Checkout\Cart\Order\OrderConvertedEvent;
  16. use Shopware\Core\Framework\Struct\ArrayEntity;
  17. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextRestorer;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. class OrderConvertedSubscriber implements EventSubscriberInterface
  21. {
  22.     private OfferService $offerService;
  23.     private RequestStack $requestStack;
  24.     private SalesChannelContextRestorer $salesChannelContextRestorer;
  25.     public function __construct(
  26.         OfferService $offerService,
  27.         SalesChannelContextRestorer $salesChannelContextRestorer,
  28.         RequestStack $requestStack
  29.     )
  30.     {
  31.         $this->offerService $offerService;
  32.         $this->requestStack $requestStack;
  33.         $this->salesChannelContextRestorer $salesChannelContextRestorer;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             CartConvertedEvent::class => 'onCartConvertedEvent',
  39.             CheckoutOrderPlacedEvent::class => ['resetToOriginalCart', -1000],
  40.             OrderConvertedEvent::class => 'onOrderConvertedEvent',
  41.         ];
  42.     }
  43.     /**
  44.      * Flag offer as ordered. Close offer mode
  45.      * @param CartConvertedEvent $event
  46.      */
  47.     public function onCartConvertedEvent(CartConvertedEvent $event): void
  48.     {
  49.         $salesChannelContext $event->getSalesChannelContext();
  50.         $request $this->requestStack->getMainRequest();
  51.         if (!$request instanceof RequestStack) {
  52.             return;
  53.         }
  54.         $session $request->getSession();
  55.         if ($offerMode $this->offerService->getOfferMode($salesChannelContext$session)) {
  56.             $offer $this->offerService->getOfferById($offerMode$event->getContext());
  57.             $this->offerService->disableOfferMode($salesChannelContext);
  58.             $this->offerService->acceptOffer($offer$event->getContext());
  59.             $customFields $event->getConvertedCart()['customFields'] ?? [];
  60.             $customFields[PremsIndividualOffer6::ORDER_FROM_OFFER] = true;
  61.             $customFields[PremsIndividualOffer6::ORDER_FROM_OFFER_ID] = $offer->getId();
  62.             $cartData $event->getOriginalConvertedCart();
  63.             $cartData['customFields'] = $customFields;
  64.             $event->setConvertedCart($cartData);
  65.         }
  66.     }
  67.     public function resetToOriginalCart(CheckoutOrderPlacedEvent $event): void
  68.     {
  69.         if (method_exists($this->salesChannelContextRestorer,'restoreByOrder')) {
  70.             $salesChannelContext $this->salesChannelContextRestorer->restoreByOrder($event->getOrderId(), $event->getContext());
  71.             $request $this->requestStack->getMainRequest();
  72.             if (!$request instanceof RequestStack) {
  73.                 return;
  74.             }
  75.             $session $request->getSession();
  76.             $this->offerService->restoreBackupCart($salesChannelContext$session);
  77.         }
  78.     }
  79.     public function onOrderConvertedEvent(OrderConvertedEvent $event): void
  80.     {
  81.         $customFields $event->getOrder()->getCustomFields() ?? [];
  82.         if (isset($customFields[PremsIndividualOffer6::ORDER_FROM_OFFER])) {
  83.             $event->getConvertedCart()->addExtension(PremsIndividualOffer6::OFFER_MODE_EXTENSION_NAME, new ArrayEntity(['id' => $customFields[PremsIndividualOffer6::ORDER_FROM_OFFER_ID]]));
  84.         }
  85.     }
  86. }