vendor/store.shopware.com/viocustomerprice/src/Subscriber/ProductSubscriber.php line 62

Open in your IDE?
  1. <?php /** @noinspection PhpMissingFieldTypeInspection */
  2. declare(strict_types=1);
  3. namespace VioCustomerPrice\Subscriber;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\Content\Product\ProductDefinition;
  6. use Shopware\Core\Content\Product\ProductEntity;
  7. use Shopware\Core\Content\Product\ProductEvents;
  8. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\PlatformRequest;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. use Symfony\Component\HttpFoundation\Request;
  19. class ProductSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var RequestStack
  23.      */
  24.     private $requestStack;
  25.     /**
  26.      * @var EntityRepositoryInterface
  27.      */
  28.     private $customerPriceRepository;
  29.     /**
  30.      * ProductSubscriber constructor.
  31.      * @param RequestStack $requestStack
  32.      * @param EntityRepositoryInterface $customerPriceRepository
  33.      */
  34.     public function __construct(
  35.         RequestStack $requestStack,
  36.         EntityRepositoryInterface $customerPriceRepository
  37.     )
  38.     {
  39.         $this->requestStack $requestStack;
  40.         $this->customerPriceRepository $customerPriceRepository;
  41.     }
  42.     /**
  43.      * @return array The event names to listen to
  44.      */
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductLoaded'
  49.         ];
  50.     }
  51.     /**
  52.      * @param EntityLoadedEvent $event
  53.      */
  54.     public function onProductLoaded(EntityLoadedEvent $event): void
  55.     {
  56.         if( $event->getDefinition() instanceof ProductDefinition
  57.             && $this->requestStack->getCurrentRequest() instanceof Request
  58.         ){
  59.             $source $event->getContext()->getSource();
  60.             if($source instanceof SalesChannelApiSource) {
  61.                 /** @var SalesChannelContext $salesChannelContext */
  62.                 $salesChannelContext $this->requestStack->getCurrentRequest()->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  63.                 if( $salesChannelContext instanceof SalesChannelContext
  64.                     && $salesChannelContext->getCustomer() instanceof CustomerEntity
  65.                 ) {
  66.                     $customerPricesResult $this->customerPriceRepository
  67.                         ->search(
  68.                             (new Criteria())
  69.                             ->addFilter(
  70.                                 new EqualsFilter('customerId'$salesChannelContext->getCustomer()->getId()),
  71.                                 new EqualsAnyFilter('productId'$event->getIds())
  72.                             ),
  73.                             $event->getContext()
  74.                         );
  75.                     /** @var ProductEntity $product */
  76.                     foreach ($event->getEntities() as $product) {
  77.                         if(!$product->hasExtension('customerPrices')) {
  78.                             $customerPrices $customerPricesResult->filterByProperty('productId'$product->getId());
  79.                             /** @noinspection PhpPossiblePolymorphicInvocationInspection */
  80.                             $product->addExtension('customerPrices'$customerPrices->getEntities());
  81.                         }
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.     }
  87. }