custom/static-plugins/WabsAuth/src/Subscriber/ProductSubscriber.php line 60

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace WabsAuth\Subscriber;
  3. use Acris\CustomerGroupAvailableProduct\Components\DisplayAllowedProductService;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\Routing\RouterInterface;
  14. class ProductSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var DisplayAllowedProductService
  18.      */
  19.     private $displayProductService;
  20.     /**
  21.      * @var EntityRepositoryInterface
  22.      */
  23.     private $productCustomerGroupRepository;
  24.     /**
  25.      * @var EntityRepositoryInterface
  26.      */
  27.     private $customerGroupRepository;
  28.     /**
  29.      * @var SystemConfigService
  30.      */
  31.     private $configService;
  32.     /**
  33.      * @var RouterInterface
  34.      */
  35.     private $router;
  36.     public function __construct(DisplayAllowedProductService $displayProductServiceEntityRepositoryInterface $productCustomerGroupRepositoryEntityRepositoryInterface $customerGroupRepositorySystemConfigService $configServiceRouterInterface $router)
  37.     {
  38.         $this->displayProductService $displayProductService;
  39.         $this->productCustomerGroupRepository $productCustomerGroupRepository;
  40.         $this->customerGroupRepository $customerGroupRepository;
  41.         $this->configService $configService;
  42.         $this->router $router;
  43.     }
  44.     public static function getSubscribedEvents(): array
  45.     {
  46.         return[
  47.             ProductPageLoadedEvent::class => ['onProductLoaded'9999]
  48.         ];
  49.     }
  50.     public function onProductLoaded(ProductPageLoadedEvent $event)
  51.     {
  52.         if( !$event->getRequest()->get('checkLogin') || $event->getSalesChannelContext()->getCustomerId()) {
  53.             return;
  54.         }
  55.         $productId $event->getPage()->getProduct()->getId();
  56.         if(empty($productId)) {
  57.             return;
  58.         }
  59.         if($this->configService->get('AcrisCustomerGroupAvailableProductCS.config.releaseProductsIfNoCustomerGroupAssigned'$event->getSalesChannelContext()->getSalesChannel()->getId()) === DisplayAllowedProductService::DEFAULT_PLUGIN_CONFIG_RELEASE_PRODUCT_IF_NO_CUSTOMER_GROUPS_ASSIGNED && $this->displayProductService->checkIfNoCustomerGroupsAssigned($productId$event->getSalesChannelContext()->getContext())) {
  60.             return;
  61.         }
  62.         $displayProductIdsResult $this->productCustomerGroupRepository->searchIds(
  63.             (new Criteria())
  64.                 ->addFilter(new EqualsFilter('productId'$productId))
  65.                 ->addAssociation('customerGroup')
  66.             , $event->getContext()
  67.         );
  68.         if($displayProductIdsResult) {
  69.             $displayIds $displayProductIdsResult->getIds();
  70.             $customerGroupIds = [];
  71.             foreach ($displayIds as $displayId) {
  72.                 $customerGroupIds[] = $displayId['customerGroupId'];
  73.             }
  74.             $customerGroupsResult $this->customerGroupRepository->search(
  75.                 (new Criteria($customerGroupIds)),
  76.                 $event->getContext()
  77.             );
  78.             $customerGroupNames = [];
  79.             if($customerGroupsResult) {
  80.                 foreach($customerGroupsResult as $customerGroup) {
  81.                     $customerGroupNames[] =  $customerGroup->getName();
  82.                 }
  83.                 $currentCustomerGroup $event->getSalesChannelContext()->getCurrentCustomerGroup()->getName();
  84.                 if(!in_array($currentCustomerGroup$customerGroupNames)) {
  85.                     dump($currentCustomerGroup);
  86.                     dump($customerGroupNames);
  87.                     $redirectResponse = new RedirectResponse($this->router->generate('frontend.account.oauth.redirect', [
  88.                         'redirect_to' => $event->getRequest()->getUri(),
  89.                         'auto_group' => $customerGroupNames
  90.                     ]));
  91.                     $redirectResponse->send();
  92.                     exit;
  93.                 }
  94.             }
  95.         }
  96.     }
  97. }