custom/plugins/econdWebAnalytics/src/Storefront/Page/Product/Subscriber/ProductPageLoadedEventSubscriber.php line 50

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Ecd\econdWebAnalytics\Storefront\Page\Product\Subscriber;
  3. use Shopware\Core\Framework\Struct\ArrayEntity;
  4. use Shopware\Core\System\SystemConfig\SystemConfigService;
  5. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class ProductPageLoadedEventSubscriber implements EventSubscriberInterface {
  8.     private $systemConfigService$breadcrumbHelper;
  9.     public function __construct(SystemConfigService $systemConfigService)
  10.     {
  11.         $this->systemConfigService $systemConfigService;
  12.     }
  13.     /**
  14.      * @inheritDoc
  15.      */
  16.     public static function getSubscribedEvents()
  17.     {
  18.         return [
  19.             ProductPageLoadedEvent::class => 'onProductPageLoadedEvent'
  20.         ];
  21.     }
  22.     public function onProductPageLoadedEvent(ProductPageLoadedEvent $event) : void
  23.     {
  24.         $group '';
  25.         $config $this->systemConfigService->getDomain('econdWebAnalytics');
  26.         $page $event->getPage();
  27.         $product $page->getProduct();
  28.         $categoryTree $product->getCategoryTree();
  29.         $navigationTree $page->getHeader()->getNavigation()->getTree();
  30.         if($categoryTree != null) {
  31.             foreach (array_reverse($categoryTree) as $catId) {
  32.                 $catTreeItem $this->findTreeItemByCategoryId($catId$navigationTree);
  33.                 if ($catTreeItem) break;
  34.             }
  35.             $breadcrumb $catTreeItem->getCategory()->getPlainBreadcrumb();
  36.             // remove first element of array
  37.             array_shift($breadcrumb);
  38.             // key last key of array
  39.             $lastkey array_key_last($breadcrumb);
  40.             foreach($breadcrumb as $k => $item) {
  41.                 $group .= $item;
  42.                 if($k != $lastkey) {
  43.                     $group .= '/';
  44.                 }
  45.             }
  46.         }else {
  47.             $group null;
  48.         }
  49.         $variant = [
  50.             'econdWebAnalytics.config.var1' => '',
  51.             'econdWebAnalytics.config.var2' => '',
  52.             'econdWebAnalytics.config.var3' => ''
  53.         ];
  54.         foreach($page->getSelectedOptions() as $option){
  55.             foreach ($config as $k => &$item) {
  56.                 if($item == $option->getGroupId()) {
  57.                     $variant[$k] = $option->getName();
  58.                 }
  59.             }
  60.         }
  61.         if (strpos($product->getProductNumber(),'.') !== false) {
  62.             $pid explode('.'$product->getProductNumber())[0];
  63.         } else {
  64.             $pid $product->getProductNumber();
  65.         }
  66.         $ana = [
  67.             'pid' => $pid,
  68.             'group' => $group,
  69.             'var1' => $variant['econdWebAnalytics.config.var1'],
  70.             'var2' => $variant['econdWebAnalytics.config.var2'],
  71.             'var3' => $variant['econdWebAnalytics.config.var3']
  72.         ];
  73.         $page->addExtension('ANA', new ArrayEntity($ana));
  74.     }
  75.     private function findTreeItemByCategoryId(string $categoryId, array $tree)
  76.     {
  77.         if (isset($tree[$categoryId])) {
  78.             return $tree[$categoryId];
  79.         }
  80.         foreach ($tree as $item) {
  81.             if (count($item->getChildren()))
  82.             {
  83.                 $nested $this->findTreeItemByCategoryId($categoryId$item->getChildren());
  84.                 if ($nested) {
  85.                     return $nested;
  86.                 }
  87.             }
  88.         }
  89.         return null;
  90.     }
  91. }