custom/plugins/CbaxModulGoogleShopping/src/Subscriber/BackendSubscriber.php line 29

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cbax\ModulGoogleShopping\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Core\Content\ProductExport\Event\ProductExportRenderBodyContextEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. class BackendSubscriber implements EventSubscriberInterface
  9. {
  10.     /** Repository */
  11.     private $categoryRepository;
  12.     public function __construct(
  13.         EntityRepositoryInterface $categories
  14.     ) {
  15.         $this->categoryRepository $categories;
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return[
  20.             ProductExportRenderBodyContextEvent::class => 'onProductExportRenderBodyContext'
  21.         ];
  22.     }
  23.     public function onProductExportRenderBodyContextProductExportRenderBodyContextEvent $productExportContext$context)
  24.     {
  25.         $newContext $productExportContext->getContext();
  26.         $footerTemplate $newContext['productExport']->getFooterTemplate();
  27.         if (empty($footerTemplate)) return;
  28.         if (strpos($footerTemplate'CbaxGoogleShopping') === FALSE) return;
  29.         $navigationId $newContext['context']->getSalesChannel()->getNavigationCategoryId();
  30.         $salesChannelContext $newContext['context'];
  31.         //Get all Categories where navigationId is in path
  32.         $criteria = new Criteria();
  33.         $criteria->addFilter(new ContainsFilter('path'$navigationId));
  34.         $categoriesResult $this->categoryRepository->search($criteria$salesChannelContext->getContext());
  35.         $salesChannelCategories $this->prepareSaleChannelCategories($categoriesResult);
  36.         $newContext['cbaxSalesChannelCategories'] = $salesChannelCategories;
  37.         $productExportContext->setContext($newContext);
  38.     }
  39.     private function prepareSaleChannelCategories($categoryCollection) {
  40.         $categories = [];
  41.         foreach ($categoryCollection->getElements() as $category) {
  42.             if (!empty($category->get('translated')['customFields']['cbaxGoogleCategory'])) {
  43.                 $categories[$category->get('id')] = $category->get('translated')['customFields']['cbaxGoogleCategory'];
  44.             }
  45.         }
  46.         return $categories;
  47.     }
  48. }