custom/plugins/MadArticleCustomergroup/src/Subscriber/CategorySubscriber.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace MadArticleCustomergroup\Subscriber;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
  8. use Shopware\Storefront\Pagelet\Menu\Offcanvas\MenuOffcanvasPageletLoadedEvent;
  9. use Shopware\Core\Content\Category\CategoryEvents;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. class CategorySubscriber  implements EventSubscriberInterface
  12. {
  13.     private SystemConfigService $systemConfigService;
  14.     public function __construct(
  15.         SystemConfigService $systemConfigService
  16.     )
  17.     {
  18.         $this->systemConfigService $systemConfigService;
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             HeaderPageletLoadedEvent::class => 'onHeaderPageletLoadedEvent',
  24.             MenuOffcanvasPageletLoadedEvent::class => 'onMenuOffcanvasPageletLoadedEvent',
  25.         ];
  26.     }
  27.     public function onMenuOffcanvasPageletLoadedEvent(MenuOffcanvasPageletLoadedEvent $event){
  28.         $salesChannelContext $event->getSalesChannelContext();
  29.         if($salesChannelContext->getCustomer() != null){
  30.             if($salesChannelContext->getCustomer()->getTags() != null){
  31.                 $userTags $salesChannelContext->getCustomer()->getTags()->getElements();
  32.             }else{
  33.                 $userTags '';
  34.             }
  35.             $userGroup $salesChannelContext->getCustomer()->getGroupId();
  36.         }else{
  37.             $userTags '';
  38.             $userGroup '';
  39.         }
  40.         /* for the navigation */
  41.         foreach($event->getPagelet()->getNavigation()->getTree() as $nav){
  42.             $nav->getCategory()->MadShowCategory true;
  43.             $nav->getCategory()->MadShowTagCategory true;
  44.             if(isset($nav->getCategory()->getCustomFields()['mad_category_usergroups_enable']) and count($nav->getCategory()->getCustomFields()['mad_category_usergroups_enable']) > 0){
  45.                 /* Check if User Login */
  46.                 if($userGroup == ''){
  47.                     $nav->getCategory()->MadShowCategory false;
  48.                 }elseif(!in_array($userGroup$nav->getCategory()->getCustomFields()['mad_category_usergroups_enable'])){
  49.                     $nav->getCategory()->MadShowCategory false;
  50.                 }
  51.             }
  52.             if(isset($nav->getCategory()->getCustomFields()['mad_category_tags_enable']) and count($nav->getCategory()->getCustomFields()['mad_category_tags_enable']) > 0){
  53.                 /* Check if User Login */
  54.                 if($userTags == ''){
  55.                     $nav->getCategory()->MadShowTagCategory false;
  56.                 }else{
  57.                     foreach($nav->getCategory()->getCustomFields()['mad_category_tags_enable'] as $tag){
  58.                         if(array_key_exists($tag$userTags)){
  59.                             $nav->getCategory()->MadShowTagCategory true;
  60.                             break;
  61.                         }else{
  62.                             $nav->getCategory()->MadShowTagCategory false;
  63.                         }
  64.                     }
  65.                 }
  66.             }
  67.             /* Children Category */
  68.             if(count($nav->getChildren()) > 0){
  69.                 foreach($nav->getChildren() as $child){
  70.                     $child->getCategory()->MadShowCategory true;
  71.                     $child->getCategory()->MadShowTagCategory true;
  72.                     if(isset($child->getCategory()->getCustomFields()['mad_category_usergroups_enable']) and count($child->getCategory()->getCustomFields()['mad_category_usergroups_enable']) > 0){
  73.                         /* Check if User Login */
  74.                         if($userGroup == ''){
  75.                             $child->getCategory()->MadShowCategory false;
  76.                         }elseif(!in_array($userGroup$child->getCategory()->getCustomFields()['mad_category_usergroups_enable'])){
  77.                             $child->getCategory()->MadShowCategory false;
  78.                         }
  79.                     }
  80.                     if(isset($child->getCategory()->getCustomFields()['mad_category_tags_enable']) and count($child->getCategory()->getCustomFields()['mad_category_tags_enable']) > 0){
  81.                         /* Check if User Login */
  82.                         if($userTags == ''){
  83.                             $child->getCategory()->MadShowTagCategory false;
  84.                         }else{
  85.                             foreach($child->getCategory()->getCustomFields()['mad_category_tags_enable'] as $tag){
  86.                                 if(array_key_exists($tag$userTags)){
  87.                                     $child->getCategory()->MadShowTagCategory true;
  88.                                     break;
  89.                                 }else{
  90.                                     $child->getCategory()->MadShowTagCategory false;
  91.                                 }
  92.                             }
  93.                         }
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.         /* for the active category */
  99.         $activeCategory $event->getPagelet()->getNavigation()->getActive();
  100.         $activeCategory->MadShowCategory true;
  101.         $activeCategory->MadShowTagCategory true;
  102.     }
  103.     public function onHeaderPageletLoadedEvent(HeaderPageletLoadedEvent $event){
  104.         $salesChannelContext $event->getSalesChannelContext();
  105.         if($salesChannelContext->getCustomer() != null){
  106.             if($salesChannelContext->getCustomer()->getTags() != null){
  107.                 $userTags $salesChannelContext->getCustomer()->getTags()->getElements();
  108.             }else{
  109.                 $userTags '';
  110.             }
  111.             $userGroup $salesChannelContext->getCustomer()->getGroupId();
  112.         }else{
  113.             $userTags '';
  114.             $userGroup $this->systemConfigService->getString('MadArticleCustomergroup.config.guestGroup');
  115.         }
  116.         /* for the navigation */
  117.         foreach($event->getPagelet()->getNavigation()->getTree() as $nav){
  118.             $nav->getCategory()->MadShowCategory true;
  119.             $nav->getCategory()->MadShowTagCategory true;
  120.             if(isset($nav->getCategory()->getCustomFields()['mad_category_usergroups_enable']) and count($nav->getCategory()->getCustomFields()['mad_category_usergroups_enable']) > 0){
  121.                 /* Check if User Login */
  122.                 if($userGroup == ''){
  123.                     $nav->getCategory()->MadShowCategory false;
  124.                 }elseif(!in_array($userGroup$nav->getCategory()->getCustomFields()['mad_category_usergroups_enable'])){
  125.                     $nav->getCategory()->MadShowCategory false;
  126.                 }
  127.             }
  128.             if(isset($nav->getCategory()->getCustomFields()['mad_category_tags_enable']) and count($nav->getCategory()->getCustomFields()['mad_category_tags_enable']) > 0){
  129.                 /* Check if User Login */
  130.                 if($userTags == ''){
  131.                     $nav->getCategory()->MadShowTagCategory false;
  132.                 }else{
  133.                     foreach($nav->getCategory()->getCustomFields()['mad_category_tags_enable'] as $tag){
  134.                         if(array_key_exists($tag$userTags)){
  135.                             $nav->getCategory()->MadShowTagCategory true;
  136.                             break;
  137.                         }else{
  138.                             $nav->getCategory()->MadShowTagCategory false;
  139.                         }
  140.                     }
  141.                 }
  142.             }
  143.             /* Children Category */
  144.             if(count($nav->getChildren()) > 0){
  145.                 foreach($nav->getChildren() as $child){
  146.                     $child->getCategory()->MadShowCategory true;
  147.                     $child->getCategory()->MadShowTagCategory true;
  148.                     if(isset($child->getCategory()->getCustomFields()['mad_category_usergroups_enable']) and count($child->getCategory()->getCustomFields()['mad_category_usergroups_enable']) > 0){
  149.                         /* Check if User Login */
  150.                         if($userGroup == ''){
  151.                             $child->getCategory()->MadShowCategory false;
  152.                         }elseif(!in_array($userGroup$child->getCategory()->getCustomFields()['mad_category_usergroups_enable'])){
  153.                             $child->getCategory()->MadShowCategory false;
  154.                         }
  155.                     }
  156.                     if(isset($child->getCategory()->getCustomFields()['mad_category_tags_enable']) and count($child->getCategory()->getCustomFields()['mad_category_tags_enable']) > 0){
  157.                         /* Check if User Login */
  158.                         if($userTags == ''){
  159.                             $child->getCategory()->MadShowTagCategory false;
  160.                         }else{
  161.                             foreach($child->getCategory()->getCustomFields()['mad_category_tags_enable'] as $tag){
  162.                                 if(array_key_exists($tag$userTags)){
  163.                                     $child->getCategory()->MadShowTagCategory true;
  164.                                     break;
  165.                                 }else{
  166.                                     $child->getCategory()->MadShowTagCategory false;
  167.                                 }
  168.                             }
  169.                         }
  170.                     }
  171.                 }
  172.             }
  173.         }
  174.         /* for the active category */
  175.         $activeCategory $event->getPagelet()->getNavigation()->getActive();
  176.         $activeCategory->MadShowCategory true;
  177.         $activeCategory->MadShowTagCategory true;
  178.         if($userGroup == ''){
  179.             $activeCategory->MadLogin false;
  180.         }else{
  181.             $activeCategory->MadLogin true;
  182.         }
  183.         if(isset($activeCategory->getCustomFields()['mad_category_usergroups_enable']) and count($activeCategory->getCustomFields()['mad_category_usergroups_enable']) > 0){
  184.             /* Check if User Login */
  185.             if($userGroup == ''){
  186.                 $activeCategory->MadShowCategory false;
  187.             }elseif(!in_array($userGroup$activeCategory->getCustomFields()['mad_category_usergroups_enable'])){
  188.                 $activeCategory->MadShowCategory false;
  189.             }
  190.         }
  191.         if(isset($activeCategory->getCustomFields()['mad_category_tags_enable']) and count($activeCategory->getCustomFields()['mad_category_tags_enable']) > 0){
  192.             /* Check if User Login */
  193.             if($userTags == ''){
  194.                 $activeCategory->MadShowTagCategory false;
  195.             }else{
  196.                 foreach($activeCategory->getCustomFields()['mad_category_tags_enable'] as $tag){
  197.                     if(array_key_exists($tag$userTags)){
  198.                         $activeCategory->MadShowTagCategory true;
  199.                         break;
  200.                     }else{
  201.                         $activeCategory->MadShowTagCategory false;
  202.                     }
  203.                 }
  204.             }
  205.         }
  206.     }
  207. }