custom/plugins/SmsBecoTechnicTheme/src/Subscriber/CategorySubscriber.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SmsBecoTechnicTheme\Subscriber;
  3. use Acris\CategoryCustomerGroup\Components\BlockCategoryService;
  4. use Shopware\Core\Content\Category\CategoryEntity;
  5. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeSentEvent;
  6. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
  7. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  8. use Shopware\Core\Framework\Api\Context\AdminSalesChannelApiSource;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  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\Storefront\Page\Product\ProductPageLoadedEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class CategorySubscriber implements EventSubscriberInterface
  18. {
  19.     private EntityRepository $categoryRepository;
  20.     private EntityRepository $blockedCategoryRepository;
  21.     public function __construct(
  22.         EntityRepository $categoryRepository,
  23.         EntityRepository $blockedCategoryRepository
  24.     )
  25.     {
  26.         $this->categoryRepository $categoryRepository;
  27.         $this->blockedCategoryRepository $blockedCategoryRepository;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             'category.written' => 'onCategoryWrittenEvent',
  33.         ];
  34.     }
  35.     public function onCategoryWrittenEvent(EntityWrittenEvent $event)
  36.     {
  37.         $contextSource $event->getContext()->getSource();
  38.         // Only trigger on Admin changes
  39.         if (!$contextSource instanceof AdminApiSource) {
  40.             return;
  41.         }
  42.         ini_set('max_execution_time'"0");
  43.         $criteria = new Criteria();
  44.         $criteria->addFilter(new EqualsAnyFilter('id'$event->getIds()));
  45.         $criteria->addAssociation('children');
  46.         /** @var CategoryEntity $category */
  47.         $category $this->categoryRepository->search($criteria$event->getContext())->first();
  48.         $categoryCustomFields $category->getCustomFields();
  49.         if (isset($categoryCustomFields['cdz_use_configuration_for_child_categories']) && $categoryCustomFields['cdz_use_configuration_for_child_categories'] && $category->getChildren()->count() > 0) {
  50.             $blockedCustomerGroupsCriteria = new Criteria();
  51.             $blockedCustomerGroupsCriteria->addFilter(new EqualsFilter('categoryId'$category->getId()));
  52.             $blockedCustomerGroups $this->blockedCategoryRepository->searchIds($blockedCustomerGroupsCriteria$event->getContext())->getIds();
  53.             if (empty($blockedCustomerGroups)) {
  54.                 return;
  55.             }
  56.             $this->updateChildCategories($category$event$blockedCustomerGroups);
  57.         }
  58.     }
  59.     private function updateChildCategories(CategoryEntity $categoryEntityWrittenEvent $event$blockedCustomerGroups): void
  60.     {
  61.         $childCategories $this->getChildCategories($category$event);
  62.         foreach ($childCategories as $childCategory) {
  63.             $this->updateChildCategories($childCategory$event$blockedCustomerGroups);
  64.         }
  65.         foreach ($blockedCustomerGroups as $blockedCustomerGroup) {
  66.             $this->blockedCategoryRepository->create([
  67.                 [
  68.                     'categoryId' => $category->getId(),
  69.                     'customerGroupId' => $blockedCustomerGroup['customerGroupId']
  70.                 ]
  71.             ], $event->getContext());
  72.         }
  73.     }
  74.     private function getChildCategories(CategoryEntity $categoryEntityWrittenEvent $event): EntityCollection
  75.     {
  76.         $criteria = new Criteria();
  77.         $criteria->addFilter(new EqualsFilter('parentId'$category->getId()));
  78.         $criteria->addAssociation('children');
  79.         return $this->categoryRepository->search($criteria$event->getContext())->getEntities();
  80.     }
  81. }