custom/plugins/cutvertSeo/src/Subscriber/SeoSubscriber.php line 60

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace cutvert\Seo\Subscriber;
  3. use ECSPrefix20211002\Symfony\Component\HttpFoundation\Request;
  4. use PhpParser\Node\Expr\BinaryOp\Equal;
  5. use Shopware\Core\Content\Category\Tree\Tree;
  6. use Shopware\Core\Content\Category\Tree\TreeItem;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\AndFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  13. use Shopware\Core\System\SystemConfig\SystemConfigService;
  14. use Shopware\Storefront\Event\StorefrontRenderEvent;
  15. use Shopware\Storefront\Page\Navigation\NavigationPage;
  16. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Shopware\Core\Content\Product\ProductEvents;
  19. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  20. class SeoSubscriber implements EventSubscriberInterface
  21. {
  22.     /** @var SystemConfigService */
  23.     private $systemConfigService;
  24.     private EntityRepositoryInterface $faqRepository;
  25.     /**
  26.      * @var EntityRepositoryInterface
  27.      */
  28.     private EntityRepositoryInterface $categoryRepo;
  29.     private EntityRepositoryInterface $productRepo;
  30.     public function __construct(SystemConfigService $systemConfigService,EntityRepositoryInterface $faqRepository,EntityRepositoryInterface $categoryRepo,EntityRepositoryInterface $productRepo)
  31.     {
  32.         $this->systemConfigService $systemConfigService;
  33.         $this->faqRepository $faqRepository;
  34.         $this->categoryRepo $categoryRepo;
  35.         $this->productRepo $productRepo;
  36.     }
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductsLoaded',
  41.             // NavigationPageLoadedEvent::class => "foobar",
  42.             StorefrontRenderEvent::class => 'onStorefrontRender'
  43.         ];
  44.     }
  45.     public function onProductsLoaded(EntityLoadedEvent $event): void
  46.     {
  47.         $this->SeoSettings $this->systemConfigService->get('cutvertSeo.config');
  48.     }
  49.     public function onStorefrontRender(StorefrontRenderEvent $event){
  50.         if (isset($event->getParameters()['page']) && property_exists($event->getParameters()['page'],'navigationId')){
  51.             $categoryId $event->getParameters()['page']->getNavigationId();
  52.             /** @var $page NavigationPage */
  53.             $page $event->getParameters()['page'];
  54.             $categoryTree $page->getHeader()->getNavigation();
  55.             $categoryIds $this->flatCategoryIds($categoryTree);
  56.             $criteria = new Criteria();
  57.             $criteria->addAssociation("seoUrls");
  58.             $criteria->setIds($categoryIds);
  59.             $searchResult $this->categoryRepo->search($criteria$event->getContext());
  60.             $categories $searchResult->getEntities();
  61.             $cates = [];
  62.             /** @var Request $request */
  63.             $request $event->getRequest();
  64.             $url str_replace($request->getRequestUri(),'/',$request->getUri());
  65.             foreach ($categories as $key=>$category){
  66.                 if (!empty($category->getSeoUrls()->first())) {
  67.                     $cates[$category->getParentId()][] = [
  68.                         "url" => $url $category->getSeoUrls()->first()->getSeoPathInfo(),
  69.                         "name" => $category->getName()
  70.                     ];
  71.                 }
  72.             }
  73.             $event->setParameter('cutvertEventList',$event->getParameters()['page']);
  74.             $event->setParameter('cutvertNavigationId',$categoryId);
  75.             $event->setParameter('cutvertSeoConfig',$this->systemConfigService->get('cutvertSeo.config'));
  76.             if (isset($cates[$categoryId])){
  77.                 $criteria = new Criteria();
  78.                 $criteria->addFilter(new EqualsFilter('category_id',$categoryId));
  79.                 $faqs $this->faqRepository->search($criteria,$event->getContext());
  80.                 $criteria = new Criteria();
  81.                 $criteria->addFilter(new EqualsFilter('categoryTree',$categoryId));
  82.                 $criteria->addAssociation("media");
  83.                 $products $this->productRepo->search($criteria,$event->getContext());
  84.                 $productsInCategory = [];
  85.                 $i 1;
  86.                 $productsInCategory['minPrice'] = '';
  87.                 $productsInCategory['rating'] = 0;
  88.                 $productsInCategory['ratingCount'] = 0;
  89.                 $productMedias = [];
  90.                 foreach ($products->getElements() as $product){
  91.                     $rating 0;
  92.                     if ($product->getRatingAverage() != NULL) {
  93.                         $rating $product->getRatingAverage();
  94.                         $productsInCategory['ratingCount'] = $i;
  95.                         $i++;
  96.                     }
  97.                     if (count($productMedias) < 3){
  98.                         if (isset(array_values($product->getMedia()->getElements())[0]))
  99.                             $productMedias[] = array_values($product->getMedia()->getElements())[0]->getMedia()->getUrl();
  100.                     }
  101.                     $productsInCategory['rating'] = $productsInCategory['rating'] + $rating;
  102.                     if ($productsInCategory['minPrice'] == '' ||array_values($product->getCheapestPrice()->getPrice()->getElements())[0]->getGross() < $productsInCategory['minPrice']) {
  103.                         $productsInCategory['minPrice'] = array_values($product->getCheapestPrice()->getPrice()->getElements())[0]->getGross();
  104.                     }
  105.                 }
  106.                 $productsInCategory['absoluteRating'] = 0;
  107.                 if ($productsInCategory['ratingCount'] > 0) {
  108.                     $productsInCategory['absoluteRating'] = $productsInCategory['rating'] / $productsInCategory['ratingCount'];
  109.                 }
  110.                 $event->setParameter('cutvertShopURL',$url);
  111.                 $event->setParameter('cutvertFaqs',$faqs->getEntities()->getElements());
  112.                 $event->setParameter('cutvertProductRepo',$productsInCategory);
  113.                 $event->setParameter("cutvertSeoUrls"$cates[$categoryId]);
  114.                 $event->setParameter('cutvertImages',$productMedias);
  115.             }
  116.         }
  117.     }
  118.     private function getRootCategory($categories, &$children = [], $currentId=''){
  119.         /**
  120.          * @var string $categoryId
  121.          * @var TreeItem $category
  122.         */
  123.         foreach($categories as $categoryId => $category){
  124.             if(count($category->getChildren()) > 0){
  125.                 if($category->getCategory()->getId() == $currentId){
  126.                     $children[] = $category;
  127.                 }
  128.                 $this->getRootCategory($category->getChildren(), $children$currentId);
  129.             }
  130.         }
  131.         return $children;
  132.     }
  133.     private function flatCategoryIds(Tree $categoryTree): array
  134.     {
  135.         $ids = [];
  136.         $this->flatCategoryIdArm($categoryTree->getTree(), $ids);
  137.         return $ids;
  138.     }
  139.     /**
  140.      * @param TreeItem[] $tree
  141.      * @param array $ids
  142.      */
  143.     private function flatCategoryIdArm(array $tree, array &$ids)
  144.     {
  145.         foreach($tree as $item) {
  146.             $ids[] = $item->getCategory()->getId();
  147.             $this->flatCategoryIdArm($item->getChildren(), $ids);
  148.         }
  149.     }
  150. }