custom/plugins/MoorlProductPromo/src/Subscriber/StorefrontSubscriber.php line 62

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlProductPromo\Subscriber;
  3. use MoorlProductPromo\Core\Service\ProductPromoService;
  4. use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
  5. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates;
  6. use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent;
  7. use Shopware\Core\Checkout\Order\OrderStates;
  8. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  9. use Shopware\Core\Defaults;
  10. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  13. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  14. use Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class StorefrontSubscriber implements EventSubscriberInterface
  17. {
  18.     private ProductPromoService $productPromoService;
  19.     public function __construct(
  20.         ProductPromoService $productPromoService
  21.     )
  22.     {
  23.         $this->productPromoService $productPromoService;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             'sales_channel.product.loaded' => 'loaded',
  29.             CheckoutOrderPlacedEvent::class => 'onCheckoutOrderPlaced',
  30.             StateMachineTransitionEvent::class => 'stateChanged',
  31.             OrderStateMachineStateChangeEvent::class => 'orderStateChanged',
  32.             'moorl_pp.written' => 'onProductPromoWritten'
  33.         ];
  34.     }
  35.     public function onProductPromoWritten(EntityWrittenEvent $event): void
  36.     {
  37.         foreach ($event->getWriteResults() as $result) {
  38.             if (!$result->hasPayload('id')) {
  39.                 continue;
  40.             }
  41.             $payload $result->getPayload();
  42.             $payload['id'];
  43.             $promo $this->productPromoService->getProductPromotionById($payload['id']);
  44.             $this->productPromoService->invalidateProductIds([$promo->getProductId()]);
  45.         }
  46.     }
  47.     public function onCheckoutOrderPlaced(CheckoutOrderPlacedEvent $event) {
  48.         $this->productPromoService->setContext($event->getContext());
  49.         $this->productPromoService->addPurchased($event->getOrder(), 'orderPlaced');
  50.     }
  51.     public function orderStateChanged(OrderStateMachineStateChangeEvent $event): void
  52.     {
  53.         $order $event->getOrder();
  54.         $transactions $order->getTransactions();
  55.         if ($transactions === null) {
  56.             return;
  57.         }
  58.         $transaction $transactions->last();
  59.         if ($transaction === null) {
  60.             return;
  61.         }
  62.         $stateMachineState $transaction->getStateMachineState();
  63.         if ($stateMachineState === null) {
  64.             return;
  65.         }
  66.         if ($stateMachineState->getTechnicalName() === OrderTransactionStates::STATE_PAID) {
  67.             $this->productPromoService->setContext($event->getContext());
  68.             $this->productPromoService->addPurchased($order'orderStatePaid');
  69.         }
  70.     }
  71.     public function stateChanged(StateMachineTransitionEvent $event): void
  72.     {
  73.         if ($event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
  74.             return;
  75.         }
  76.         if ($event->getEntityName() === 'order_transaction' && $event->getToPlace()->getTechnicalName() === OrderTransactionStates::STATE_PAID) {
  77.             $this->productPromoService->setContext($event->getContext());
  78.             $this->productPromoService->addPurchasedOrderTransactionId($event->getEntityId(), 'orderTransactionStatePaid');
  79.         }
  80.         if ($event->getEntityName() === 'order' && $event->getToPlace()->getTechnicalName() === OrderStates::STATE_COMPLETED) {
  81.             $this->productPromoService->setContext($event->getContext());
  82.             $this->productPromoService->addPurchasedOrderId($event->getEntityId(), 'orderStateCompleted');
  83.         }
  84.     }
  85.     public function loaded(SalesChannelEntityLoadedEvent $event): void
  86.     {
  87.         $this->productPromoService->setSalesChannelContext($event->getSalesChannelContext());
  88.         $this->productPromoService->updateProductDiscounts(); // TODO: put in Cronjob
  89.         /** @var SalesChannelProductEntity $product */
  90.         foreach ($event->getEntities() as $product) {
  91.             $this->productPromoService->addExtensionToProduct($product);
  92.         }
  93.     }
  94.     public function onEntityLoaded(EntityLoadedEvent $event): void
  95.     {
  96.         $source $event->getContext()->getSource();
  97.         if (!$source instanceof SalesChannelApiSource) {
  98.             return;
  99.         }
  100.         foreach ($event->getEntities() as $entity) {
  101.             if ($entity->getType() == 'moorl-product-promo') {
  102.                $this->productPromoService->setContext($event->getContext());
  103.                $this->productPromoService->initProductPromotions();
  104.                $entity->setData($this->productPromoService->getProductPromotions());
  105.             }
  106.         }
  107.     }
  108. }