custom/plugins/CogiFooterKit/src/Subscriber/FooterKitSubscriber.php line 62

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cogi\CogiFooterKit\Subscriber;
  3. use Cogi\CogiFooterKit\Core\Content\FooterKitEntity;
  4. use Shopware\Core\Content\Media\MediaEntity;
  5. use Shopware\Core\Content\Product\ProductEntity;
  6. use Shopware\Core\Content\Product\SalesChannel\Detail\AbstractProductDetailRoute;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  12. use Shopware\Core\Framework\Struct\ArrayEntity;
  13. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. class FooterKitSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var EntityRepositoryInterface
  20.      */
  21.     private $footerKitRepository;
  22.     /**
  23.      * @var EntityRepositoryInterface
  24.      */
  25.     private $mediaRepository;
  26.     /**
  27.      * @var AbstractProductDetailRoute
  28.      */
  29.     private $productDetailRoute;
  30.     /** 
  31.      * @var EntityRepositoryInterface
  32.      */
  33.     private $productRepository;
  34.     
  35.     public function __construct(
  36.         EntityRepositoryInterface $footerKitRepository
  37.         EntityRepositoryInterface $mediaRepository,
  38.         AbstractProductDetailRoute $productDetailRoute,
  39.         EntityRepositoryInterface $productRepository)
  40.     {
  41.         $this->footerKitRepository $footerKitRepository;
  42.         $this->mediaRepository $mediaRepository;
  43.         $this->productDetailRoute $productDetailRoute;
  44.         $this->productRepository $productRepository;
  45.     }
  46.     public static function getSubscribedEvents(): array
  47.     {
  48.         return[
  49.             FooterPageletLoadedEvent::class => 'onHeaderLoaded'
  50.         ];
  51.     }
  52.     public function onHeaderLoaded (FooterPageletLoadedEvent $event): void
  53.     {
  54.         $criteria = new Criteria();
  55.         $criteria->addFilter(new EqualsFilter('salesChannelId'$event->getSalesChannelContext()->getSalesChannelId()));
  56.         /** @var FooterKitEntity $footerKit */
  57.         $footerKit $this->footerKitRepository->search($criteria$event->getContext())->getElements();
  58.         if(empty($footerKit)){
  59.             return;
  60.         }
  61.         $mediaCriteria = new Criteria();
  62.         /** @var MediaEntity $media */
  63.         $media $this->mediaRepository->search($mediaCriteria$event->getContext())->getElements();
  64. //        dd(reset($footerKit));
  65.         $productIds reset($footerKit);
  66.         $newProductIds $productIds->informationConfig['dynamicProductSettings']['productIds'];
  67.         $informationType $productIds->informationConfig['basicSettings']['informationType'];
  68.         $productType $productIds->informationConfig['basicSettings']['productType'];
  69.         $salesChannel $productIds->informationConfig['basicSettings']['salesChannelNew'];
  70.         if($productType == 'new'){
  71.             $productCriteria = new Criteria();
  72.             $productCriteria->addAssociation('visibilities');
  73.             $productCriteria->addSorting(new FieldSorting("createdAt"FieldSorting::DESCENDING));
  74.             $productCriteria->setLimit(intval($productIds->informationConfig['basicSettings']['numberOfNewProduct']));
  75.             $productCriteria->addFilter(new MultiFilterMultiFilter::CONNECTION_AND,
  76.                                                         [
  77.                                                             new EqualsFilter("active"true),
  78.                                                             new EqualsFilter("visibilities.salesChannelId"$salesChannel)
  79.                                                         ]
  80.                                                     )
  81.                                                 );
  82.             /** @var ProductEntity $productEn */
  83.             $productEn $this->productRepository->search($productCriteria$event->getContext())->getIds();
  84.             $newProduct = [];
  85.             foreach($productEn as $id){
  86.                 $newProduct[] = $this->productDetailRoute->load($id , new Request(), $event->getSalesChannelContext(), new Criteria())->getProduct();
  87.             }
  88.         }
  89.         if ($informationType == 'dynamic'){
  90.             $product = [];
  91.             foreach($newProductIds as $id){
  92.                 $product[] = $this->productDetailRoute->load($id , new Request(), $event->getSalesChannelContext(), new Criteria())->getProduct();
  93.             }
  94.         }
  95.        
  96.         $page =$event->getPagelet();
  97.         $page->addExtension('CogiFooterKit', new ArrayEntity([
  98.             'footerKit' => $footerKit
  99.         ]));
  100.         $page->addExtension('Media', new ArrayEntity([
  101.             'media' => $media
  102.         ]));
  103.         if ($informationType == 'dynamic' and $productType == 'select'){
  104.             $page->addExtension('Product', new ArrayEntity(([
  105.                 'product' => $product
  106.             ])));
  107.         }
  108.         if ($informationType == 'dynamic' and $productType == 'new'){
  109.             $page->addExtension('Product', new ArrayEntity(([
  110.                 'product' =>  $newProduct
  111.             ])));
  112.         }
  113.     }
  114. }