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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DISco\GA4\Subscriber;
  3. use Exception;
  4. use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
  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\ProductListingResultEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  12. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  13. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  14. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  15. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  16. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  17. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  18. use Shopware\Storefront\Page\Search\SearchPageLoadedEvent;
  19. use Shopware\Storefront\Page\Wishlist\WishlistPageLoadedEvent;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. class FrontendSubscriber implements EventSubscriberInterface
  22. {
  23.     public function __construct()
  24.     {
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  29.         return [
  30.             ProductListingCriteriaEvent::class => 'onProductListingCriteria',
  31.             SearchPageLoadedEvent::class => 'onSearchPageLoadedEvent',
  32.             ProductPageLoadedEvent::class => 'onProductPageLoadedEvent',
  33.             NavigationPageLoadedEvent::class => 'onNavigationPageLoadedEvent',
  34.             CheckoutCartPageLoadedEvent::class => 'onCheckoutCartPageLoadedEvent',
  35.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoadedEvent',
  36.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoadedEvent',
  37.             WishlistPageLoadedEvent::class => 'onWishlistPageLoadedEvent',
  38.             OffcanvasCartPageLoadedEvent::class => 'onOffcanvasCartPageLoadedEvent'
  39.         ];
  40.     }
  41.     public function onOffcanvasCartPageLoadedEvent(OffcanvasCartPageLoadedEvent $event): bool
  42.     {
  43.         try {
  44.             $elements $event->getPage()->getCart()->getLineItems()->getElements();
  45.             if (!empty($elements)) {
  46.                 $collection array_filter($elements);
  47.                 $disGA4Extension = new LineItemCollection($collection);
  48.                 $event->getContext()->addExtension('disGA4LineItemsExtension'$disGA4Extension);
  49.             }
  50.         } catch (Exception $e) {
  51.         }
  52.         return true;
  53.     }
  54.     public function onProductListingCriteria(ProductListingCriteriaEvent $event): bool
  55.     {
  56.         $event->getCriteria()->addAssociation('categories');
  57.         $event->getCriteria()->addAssociation('seoCategory');
  58.         $event->getCriteria()->addAssociation('manufacturer');
  59.         return true;
  60.     }
  61.     public function onProductListingResultEvent(ProductListingResultEvent $event): bool
  62.     {
  63.         try {
  64.             $elements = [];
  65.             foreach ($event->getResult()->getElements() as $product) {
  66.                 $elements[] = $product;
  67.             }
  68.             if (!empty($elements)) {
  69.                 $collection array_filter($elements);
  70.                 $disGA4Extension = new EntityCollection($collection);
  71.                 $event->getContext()->addExtension('disGA4ProductsExtension'$disGA4Extension);
  72.             }
  73.         } catch (Exception $e) {
  74.         }
  75.         return true;
  76.     }
  77.     public function onWishlistPageLoadedEvent(WishlistPageLoadedEvent $event): bool
  78.     {
  79.         try {
  80.             $elements $event->getPage()->getWishlist()->getProductListing()->getElements();
  81.             if (!empty($elements)) {
  82.                 $collection array_filter($elements);
  83.                 $disGA4Extension = new EntityCollection($collection);
  84.                 $event->getContext()->addExtension('disGA4ProductsExtension'$disGA4Extension);
  85.             }
  86.         } catch (Exception $e) {
  87.         }
  88.         return true;
  89.     }
  90.     public function onSearchPageLoadedEvent(SearchPageLoadedEvent $event): bool
  91.     {
  92.         try {
  93.             $elements $event->getPage()->getListing()->getElements();
  94.             if (!empty($elements)) {
  95.                 $collection array_filter($elements);
  96.                 $disGA4Extension = new EntityCollection($collection);
  97.                 $event->getContext()->addExtension('disGA4ProductsExtension'$disGA4Extension);
  98.             }
  99.         } catch (Exception $e) {
  100.         }
  101.         return true;
  102.     }
  103.     public function onProductPageLoadedEvent(ProductPageLoadedEvent $event): bool
  104.     {
  105.         try {
  106.             $elements = [$event->getPage()->getProduct()];
  107.             $cmsList = ['product-box''product-slider''cross-selling'];
  108.             if ($event->getPage()->getProduct()->getCmsPage()) {
  109.                 try {
  110.                     $cmsElements $event->getPage()->getCmsPage()->getSections()->getBlocks()->getSlots()->getElements();
  111.                     foreach ($cmsElements as $key => $object) {
  112.                         if (!in_array($object->getType(), $cmsList)) {
  113.                             unset($cmsElements[$key]);
  114.                         }
  115.                     }
  116.                     foreach ($cmsElements as $object) {
  117.                         $data $object->getData();
  118.                         switch ($data) {
  119.                             case $data instanceof CrossSellingStruct:
  120.                                 if ($data->getCrossSellings()) {
  121.                                     foreach ($data->getCrossSellings()->getElements() as $crossSelling) {
  122.                                         foreach ($crossSelling->getProducts() as $product) {
  123.                                             $elements[] = $product;
  124.                                         }
  125.                                     }
  126.                                 }
  127.                                 break;
  128.                             case $data instanceof ProductBoxStruct:
  129.                                 $elements[] = $data->getProduct();
  130.                                 break;
  131.                             case $data instanceof ProductSliderStruct :
  132.                                 foreach ($data->getProducts() as $product) {
  133.                                     $elements[] = $product;
  134.                                 }
  135.                                 break;
  136.                             case $data instanceof ProductListingStruct:
  137.                                 foreach ($data->getListing()->getEntities() as $product) {
  138.                                     $elements[] = $product;
  139.                                 }
  140.                                 break;
  141.                         }
  142.                     }
  143.                 } catch (Exception $e) {
  144.                 }
  145.             } else {
  146.                 $crossSellings $event->getPage()->getCrossSellings();
  147.                 if (!empty($crossSellings->getElements())) {
  148.                     $crossSellingsElements $crossSellings->getElements();
  149.                     foreach ($crossSellingsElements as $crossSellingsElement) {
  150.                         foreach ($crossSellingsElement->getProducts() as $product) {
  151.                             $elements[] = $product;
  152.                         }
  153.                     }
  154.                 }
  155.             }
  156.             if (!empty($elements)) {
  157.                 $collection array_filter($elements);
  158.                 $disGA4Extension = new EntityCollection($collection);
  159.                 $event->getContext()->addExtension('disGA4ProductsExtension'$disGA4Extension);
  160.             }
  161.         } catch (Exception $e) {
  162.         }
  163.         return true;
  164.     }
  165.     public function onNavigationPageLoadedEvent(NavigationPageLoadedEvent $event): bool
  166.     {
  167.         $cmsList = ['product-box''product-listing''product-slider''cross-selling'];
  168.         $elements = [];
  169.         try {
  170.             $cmsElements $event->getPage()->getCmsPage()->getSections()->getBlocks()->getSlots()->getElements();
  171.             foreach ($cmsElements as $key => $object) {
  172.                 if (!in_array($object->getType(), $cmsList)) {
  173.                     unset($cmsElements[$key]);
  174.                 }
  175.             }
  176.             foreach ($cmsElements as $object) {
  177.                 $data $object->getData();
  178.                 switch ($data) {
  179.                     case $data instanceof CrossSellingStruct:
  180.                         if ($data->getCrossSellings()) {
  181.                             foreach ($data->getCrossSellings()->getElements() as $crossSelling) {
  182.                                 foreach ($crossSelling->getProducts() as $product) {
  183.                                     $elements[] = $product;
  184.                                 }
  185.                             }
  186.                         }
  187.                         break;
  188.                     case $data instanceof ProductBoxStruct:
  189.                         $elements[] = $data->getProduct();
  190.                         break;
  191.                     case $data instanceof ProductSliderStruct :
  192.                         foreach ($data->getProducts() as $product) {
  193.                             $elements[] = $product;
  194.                         }
  195.                         break;
  196.                     case $data instanceof ProductListingStruct:
  197.                         if ($data->getListing()) {
  198.                             foreach ($data->getListing()->getEntities() as $product) {
  199.                                 $elements[] = $product;
  200.                             }
  201.                         }
  202.                         break;
  203.                 }
  204.             }
  205.             if ($event->getPage()->getCmsPage()->getProducts()) {
  206.                 foreach ($event->getPage()->getCmsPage()->getProducts() as $product) {
  207.                     $elements[] = $product;
  208.                 }
  209.             }
  210.             if (!empty($elements)) {
  211.                 $collection array_filter($elements);
  212.                 $disGA4Extension = new EntityCollection($collection);
  213.                 $event->getContext()->addExtension('disGA4ProductsExtension'$disGA4Extension);
  214.             }
  215.         } catch (Exception $e) {
  216.         }
  217.         return true;
  218.     }
  219.     public function onCheckoutCartPageLoadedEvent(CheckoutCartPageLoadedEvent $event): bool
  220.     {
  221.         try {
  222.             $elements $event->getPage()->getCart()->getLineItems()->getElements();
  223.             if (!empty($elements)) {
  224.                 $collection array_filter($elements);
  225.                 $disGA4Extension = new LineItemCollection($collection);
  226.                 $event->getContext()->addExtension('disGA4LineItemsExtension'$disGA4Extension);
  227.             }
  228.         } catch (Exception $e) {
  229.         }
  230.         return true;
  231.     }
  232.     public function onCheckoutConfirmPageLoadedEvent(CheckoutConfirmPageLoadedEvent $event): bool
  233.     {
  234.         try {
  235.             $elements $event->getPage()->getCart()->getLineItems()->getElements();
  236.             if (!empty($elements)) {
  237.                 $collection array_filter($elements);
  238.                 $disGA4Extension = new LineItemCollection($collection);
  239.                 $event->getContext()->addExtension('disGA4LineItemsExtension'$disGA4Extension);
  240.             }
  241.         } catch (Exception $e) {
  242.         }
  243.         return true;
  244.     }
  245.     public function onCheckoutFinishPageLoadedEvent(CheckoutFinishPageLoadedEvent $event): bool
  246.     {
  247.         try {
  248.             $elements $event->getPage()->getOrder()->getLineItems()->getElements();
  249.             if (!empty($elements)) {
  250.                 $collection array_filter($elements);
  251.                 $disGA4Extension = new LineItemCollection($collection);
  252.                 $event->getContext()->addExtension('disGA4LineItemsExtension'$disGA4Extension);
  253.             }
  254.         } catch (Exception $e) {
  255.         }
  256.         return true;
  257.     }
  258. }