custom/plugins/DIScoGA4/src/Subscriber/FrontendSubscriber.php line 114

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DISco\GA4\Subscriber;
  3. use Exception;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Core\Content\Cms\SalesChannel\Struct\CrossSellingStruct;
  6. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductBoxStruct;
  7. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductListingStruct;
  8. use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductSliderStruct;
  9. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  10. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  12. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  13. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  14. use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
  15. use Shopware\Storefront\Page\Wishlist\WishlistPageLoadedEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Swag\EnterpriseSearch\Search\Page\SearchPageLoadedEvent as EnterpriseSearchPageLoadedEvent;
  18. class FrontendSubscriber implements EventSubscriberInterface
  19. {
  20.     private LoggerInterface $logger;
  21.     
  22.     public function __construct(LoggerInterface $logger)
  23.     {
  24.         $this->logger $logger;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             ProductListingCriteriaEvent::class => 'onProductListingCriteria',
  30.             ProductSearchCriteriaEvent::class => 'onProductSearchCriteriaEvent',
  31.             SearchPageLoadedEvent::class => 'onSearchPageLoadedEvent',
  32.             ProductPageLoadedEvent::class => 'onProductPageLoadedEvent',
  33.             NavigationPageLoadedEvent::class => 'onNavigationPageLoadedEvent',
  34.             EnterpriseSearchPageLoadedEvent::class => 'onEnterpriseSearchPageLoadedEvent',
  35.             WishlistPageLoadedEvent::class => 'onWishlistPageLoadedEvent'
  36.         ];
  37.     }
  38.     public function onEnterpriseSearchPageLoadedEvent(EnterpriseSearchPageLoadedEvent $event): void
  39.     {
  40.         try {
  41.             $elements $event->getPage()->getListing()->getElements();
  42.             if (empty($elements)) {
  43.                 return;
  44.             }
  45.             $collection array_filter($elements);
  46.             /* @phpstan-ignore-next-line */
  47.             $disGA4Extension = new EntityCollection($collection);
  48.             $event->getContext()->addExtension('disGA4ProductsExtension'$disGA4Extension);
  49.         } catch (Exception $e) {
  50.             $this->logger->error($e->getMessage(), ['trace' => $e->getTrace()]);
  51.         }
  52.     }
  53.     public function onProductSearchCriteriaEvent(ProductSearchCriteriaEvent $event): bool
  54.     {
  55.         $event->getCriteria()->addAssociation('categories');
  56.         $event->getCriteria()->addAssociation('seoCategory');
  57.         $event->getCriteria()->addAssociation('manufacturer');
  58.         return true;
  59.     }
  60.     public function onProductListingCriteria(ProductListingCriteriaEvent $event): bool
  61.     {
  62.         $event->getCriteria()->addAssociation('categories');
  63.         $event->getCriteria()->addAssociation('seoCategory');
  64.         $event->getCriteria()->addAssociation('manufacturer');
  65.         return true;
  66.     }
  67.     public function onWishlistPageLoadedEvent(WishlistPageLoadedEvent $event): bool
  68.     {
  69.         try {
  70.             $elements $event->getPage()->getWishlist()->getProductListing()->getElements();
  71.             if (!empty($elements)) {
  72.                 $collection array_filter($elements);
  73.                 $disGA4Extension = new EntityCollection($collection);
  74.                 $event->getContext()->addExtension('disGA4ProductsExtension'$disGA4Extension);
  75.             }
  76.         } catch (Exception $e) {
  77.             $this->logger->error($e->getMessage(), ['trace' => $e->getTrace()]);
  78.         }
  79.         return true;
  80.     }
  81.     public function onSearchPageLoadedEvent(SearchPageLoadedEvent $event): bool
  82.     {
  83.         try {
  84.             $elements $event->getPage()->getListing()->getElements();
  85.             if (!empty($elements)) {
  86.                 $collection array_filter($elements);
  87.                 $disGA4Extension = new EntityCollection($collection);
  88.                 $event->getContext()->addExtension('disGA4ProductsExtension'$disGA4Extension);
  89.             }
  90.         } catch (Exception $e) {
  91.             $this->logger->error($e->getMessage(), ['trace' => $e->getTrace()]);
  92.         }
  93.         return true;
  94.     }
  95.     public function onProductPageLoadedEvent(ProductPageLoadedEvent $event): bool
  96.     {
  97.         try {
  98.             $elements = [$event->getPage()->getProduct()];
  99.             $cmsList = ['product-box''product-slider''cross-selling'];
  100.             if ($event->getPage()->getProduct()->getCmsPage()) {
  101.                 try {
  102.                     $cmsElements $event->getPage()->getCmsPage()->getSections()->getBlocks()->getSlots()->getElements();
  103.                     foreach ($cmsElements as $key => $object) {
  104.                         if (!in_array($object->getType(), $cmsList)) {
  105.                             unset($cmsElements[$key]);
  106.                         }
  107.                     }
  108.                     foreach ($cmsElements as $object) {
  109.                         $data $object->getData();
  110.                         switch ($data) {
  111.                             case $data instanceof CrossSellingStruct:
  112.                                 if ($data->getCrossSellings()) {
  113.                                     foreach ($data->getCrossSellings()->getElements() as $crossSelling) {
  114.                                         foreach ($crossSelling->getProducts() as $product) {
  115.                                             $elements[] = $product;
  116.                                         }
  117.                                     }
  118.                                 }
  119.                                 break;
  120.                             case $data instanceof ProductBoxStruct:
  121.                                 if (null !== $data->getProduct()) {
  122.                                     $elements[] = $data->getProduct();
  123.                                 }
  124.                                 break;
  125.                             case $data instanceof ProductSliderStruct :
  126.                                 foreach ($data->getProducts() as $product) {
  127.                                     $elements[] = $product;
  128.                                 }
  129.                                 break;
  130.                             case $data instanceof ProductListingStruct:
  131.                                 foreach ($data->getListing()->getEntities() as $product) {
  132.                                     $elements[] = $product;
  133.                                 }
  134.                                 break;
  135.                         }
  136.                     }
  137.                 } catch (Exception $e) {
  138.                     $this->logger->error($e->getMessage(), ['trace' => $e->getTrace()]);
  139.                 }
  140.             } else {
  141.                 $crossSellings $event->getPage()->getCrossSellings();
  142.                 if (!empty($crossSellings->getElements())) {
  143.                     $crossSellingsElements $crossSellings->getElements();
  144.                     foreach ($crossSellingsElements as $crossSellingsElement) {
  145.                         foreach ($crossSellingsElement->getProducts() as $product) {
  146.                             $elements[] = $product;
  147.                         }
  148.                     }
  149.                 }
  150.             }
  151.             if (!empty($elements)) {
  152.                 $collection array_filter($elements);
  153.                 $disGA4Extension = new EntityCollection($collection);
  154.                 $event->getContext()->addExtension('disGA4ProductsExtension'$disGA4Extension);
  155.             }
  156.         } catch (Exception $e) {
  157.             $this->logger->error($e->getMessage(), ['trace' => $e->getTrace()]);
  158.         }
  159.         return true;
  160.     }
  161.     public function onNavigationPageLoadedEvent(NavigationPageLoadedEvent $event): bool
  162.     {
  163.         $cmsList = ['product-box''product-listing''product-slider''cross-selling'];
  164.         $elements = [];
  165.         try {
  166.             $cmsElements $event->getPage()->getCmsPage()->getSections()->getBlocks()->getSlots()->getElements();
  167.             foreach ($cmsElements as $key => $object) {
  168.                 if (!in_array($object->getType(), $cmsList)) {
  169.                     unset($cmsElements[$key]);
  170.                 }
  171.             }
  172.             foreach ($cmsElements as $object) {
  173.                 $data $object->getData();
  174.                 switch ($data) {
  175.                     case $data instanceof CrossSellingStruct:
  176.                         if ($data->getCrossSellings()) {
  177.                             foreach ($data->getCrossSellings()->getElements() as $crossSelling) {
  178.                                 foreach ($crossSelling->getProducts() as $product) {
  179.                                     $elements[] = $product;
  180.                                 }
  181.                             }
  182.                         }
  183.                         break;
  184.                     case $data instanceof ProductBoxStruct:
  185.                         if (null !== $data->getProduct()) {
  186.                             $elements[] = $data->getProduct();
  187.                         }
  188.                         break;
  189.                     case $data instanceof ProductSliderStruct :
  190.                         foreach ($data->getProducts() as $product) {
  191.                             $elements[] = $product;
  192.                         }
  193.                         break;
  194.                     case $data instanceof ProductListingStruct:
  195.                         if ($data->getListing()) {
  196.                             foreach ($data->getListing()->getEntities() as $product) {
  197.                                 $elements[] = $product;
  198.                             }
  199.                         }
  200.                         break;
  201.                 }
  202.             }
  203.             if ($event->getPage()->getCmsPage()->getProducts()) {
  204.                 foreach ($event->getPage()->getCmsPage()->getProducts() as $product) {
  205.                     $elements[] = $product;
  206.                 }
  207.             }
  208.             if (!empty($elements)) {
  209.                 $collection array_filter($elements);
  210.                 $disGA4Extension = new EntityCollection($collection);
  211.                 $event->getContext()->addExtension('disGA4ProductsExtension'$disGA4Extension);
  212.             }
  213.         } catch (Exception $e) {
  214.             $this->logger->error($e->getMessage(), ['trace' => $e->getTrace()]);
  215.         }
  216.         return true;
  217.     }
  218. }