src/Core/Subscriber/ProductSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Bodymed\Webshop\Core\Subscriber;
  4. use Bodymed\Webshop\Constants;
  5. use Bodymed\Webshop\Core\Content\CustomerProductFeature\CustomerProductFeatureEntity;
  6. use Shopware\Core\Content\Product\ProductCollection;
  7. use Shopware\Core\Content\Product\ProductDefinition;
  8. use Shopware\Core\Content\Product\ProductEvents;
  9. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  15. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory;
  16. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. use Shopware\Core\PlatformRequest;
  20. class ProductSubscriber implements EventSubscriberInterface
  21. {
  22.     private RequestStack $requestStack;
  23.     private SalesChannelContextPersister $salesChannelContextPersister;
  24.     private EntityRepositoryInterface $customerProductFeatureRepository;
  25.     public function __construct(
  26.         RequestStack $requestStack,
  27.         SalesChannelContextPersister $salesChannelContextPersister,
  28.         EntityRepositoryInterface $customerProductFeatureRepository
  29.     ) {
  30.         $this->requestStack $requestStack;
  31.         $this->salesChannelContextPersister $salesChannelContextPersister;
  32.         $this->customerProductFeatureRepository $customerProductFeatureRepository;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             ProductEvents::PRODUCT_LOADED_EVENT => 'addCustomerProductFeatureExtension'
  38.         ];
  39.     }
  40.     public function addCustomerProductFeatureExtension(EntityLoadedEvent $event): void
  41.     {
  42.         $context $event->getContext();
  43.         if ($event->getDefinition() instanceof ProductDefinition) {
  44.             /* @var $products ProductCollection */
  45.             $products $event->getEntities();
  46.             $source $event->getContext()->getSource();
  47.             if (null === $this->requestStack->getCurrentRequest()) {
  48.                 return;
  49.             }
  50.             if ($source instanceof SalesChannelApiSource) {
  51.                 $token $this->requestStack->getCurrentRequest()->headers->get(PlatformRequest::HEADER_CONTEXT_TOKEN);
  52.                 $salesChannelId $source->getSalesChannelId();
  53.                 if($salesChannelId and $token){
  54.                     $payload $this->salesChannelContextPersister->load($token$salesChannelId);
  55.                     if (isset($payload['customerId'])) {
  56.                         $criteria = (new Criteria())->addFilter(
  57.                             new EqualsFilter('customerId'$payload['customerId']),
  58.                             new EqualsAnyFilter('productId'$event->getIds())
  59.                         );
  60.                         $result $this->customerProductFeatureRepository->search($criteria$context);
  61.                         foreach ($products as $product) {
  62.                             /* @var $customerProductFeature CustomerProductFeatureEntity */
  63.                             $result $result->filterByProperty(
  64.                                 'productId',
  65.                                 $product->getId()
  66.                             )->filterByProperty('customerId'$payload['customerId']);
  67.                             $customerProductFeature $result->first();
  68.                             if ($customerProductFeature instanceof CustomerProductFeatureEntity) {
  69.                                 $product->addExtension(
  70.                                     Constants::CUSTOMER_PRODUCT_FEATURE_EXTENSION_KEY,
  71.                                     $customerProductFeature
  72.                                 );
  73.                             }
  74.                         }
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.     }
  80. }