custom/plugins/LoyxxSeminar/src/Subscriber/LineItemEventSubscriber.php line 93

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace LoyxxSeminar\Subscriber;
  3. use LoyxxSeminar\Core\Checkout\Cart\Participant\ParticipantCollection;
  4. use LoyxxSeminar\Core\Checkout\Cart\Participant\ParticipantService;
  5. use LoyxxSeminar\LoyxxSeminar;
  6. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  7. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemQuantityChangedEvent;
  8. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemRemovedEvent;
  9. use Shopware\Core\Checkout\Cart\Event\LineItemAddedEvent;
  10. use Shopware\Core\Checkout\Cart\Event\LineItemQuantityChangedEvent;
  11. use Shopware\Core\Checkout\Cart\Event\LineItemRemovedEvent;
  12. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  13. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  16. use Shopware\Storefront\Event\StorefrontRenderEvent;
  17. use Symfony\Component\EventDispatcher\EventDispatcher;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class LineItemEventSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var ParticipantService
  23.      */
  24.     protected $participantService;
  25.     /**
  26.      * @var SalesChannelRepositoryInterface
  27.      */
  28.     private $productRepository;
  29.     public function __construct(
  30.         ParticipantService $participantService,
  31.         SalesChannelRepositoryInterface $productRepository
  32.     ) {
  33.         $this->participantService $participantService;
  34.         $this->productRepository $productRepository;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             BeforeLineItemAddedEvent::class => 'onLineItemAdded',
  40.             BeforeLineItemQuantityChangedEvent::class => 'onLineItemQuantityChanged',
  41.             BeforeLineItemRemovedEvent::class => 'onLineItemRemoved'
  42.         ];
  43.     }
  44.     public function onLineItemAdded(BeforeLineItemAddedEvent $event)
  45.     {
  46.         $lineItem $event->getLineItem();
  47.         if ($lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE){ // do not process participant service for other product types
  48.             $criteria = new Criteria([$lineItem->getReferencedId()]);
  49.             $criteria->setLimit(1);
  50.             /** @var SalesChannelProductEntity $product */
  51.             $product $this->productRepository->search($criteria$event->getSalesChannelContext())->getEntities()->first();
  52.             
  53.             if ($product){
  54.                 $customFields $product->getCustomFields();
  55.                 if (isset($customFields[LoyxxSeminar::PRODUCT_CUSTOM_FIELD_SEMINAR]) &&
  56.                     $customFields[LoyxxSeminar::PRODUCT_CUSTOM_FIELD_SEMINAR]) {
  57.                 // Check if line item already exists for product
  58.                 // this happens when same product is added to cart twice
  59.                 // line item passed to event will be new, but we need to use existing line item if available
  60.                 // quantity of new line item passed will be 1 but actual quantity is in existing line item
  61.                 $lineItem $event->getCart()->getLineItems()->get($lineItem->getId()) ?? $lineItem;
  62.                 $this->participantService->syncParticipantsAndLineItemQuantity(
  63.                     $event->getCart(),
  64.                     $lineItem,
  65.                     $event->getSalesChannelContext()
  66.                 );
  67.             }
  68.             }
  69.         }
  70.     }
  71.     public function onLineItemQuantityChanged(BeforeLineItemQuantityChangedEvent $event)
  72.     {
  73.         $lineItem $event->getLineItem();
  74.         if ($lineItem->hasExtension(ParticipantCollection::PARTICIPANT_EXTENSION_NAME)) {
  75.             $this->participantService->syncParticipantsAndLineItemQuantity(
  76.                 $event->getCart(),
  77.                 $lineItem,
  78.                 $event->getSalesChannelContext()
  79.             );
  80.         }
  81.     }
  82.     public function onLineItemRemoved(BeforeLineItemRemovedEvent $event)
  83.     {
  84.         $lineItem $event->getLineItem();
  85.         if ($lineItem->hasExtension(ParticipantCollection::PARTICIPANT_EXTENSION_NAME)) {
  86.             $lineItem->removeExtension(ParticipantCollection::PARTICIPANT_EXTENSION_NAME);
  87.         }
  88.     }
  89. }