custom/plugins/CtnmFrontendHelper/src/Subscriber/Frontend.php line 170

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Ctnm\FrontendHelper\Subscriber;
  3. use Shopware\Core\Content\Product\ProductEntity;
  4. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  9. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. //use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  14. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  15. use Shopware\Core\Framework\Struct\ArrayEntity;
  16. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  17. use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
  18. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  19. use Shopware\Core\Content\Category\Tree\Tree;
  20. use Shopware\Core\Content\Cms\SalesChannel\Struct\TextStruct;
  21. class Frontend implements EventSubscriberInterface
  22. {
  23.     protected function getSeoUrls()
  24.     {
  25.         return "test";
  26.     }
  27.     /** @var ProductEntity */
  28.     protected $productRepository;
  29.     protected $standardProductRepository;
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             ProductPageLoadedEvent::class=> 'onProductLoaded',
  34.             ProductListingResultEvent::class => 'handleResult',
  35.             ProductSearchResultEvent::class => 'handleResult',
  36.             NavigationPageLoadedEvent::class=> 'onNavigationLoaded',
  37.         ];
  38.     }
  39.     public function __construct(
  40.         SalesChannelRepositoryInterface $productRepository,
  41.         EntityRepositoryInterface $standardProductRepository
  42.     )
  43.     {
  44.         $this->productRepository $productRepository;
  45.         $this->standardProductRepository $standardProductRepository;
  46.     }
  47.     public function onProductLoaded(ProductPageLoadedEvent $event)
  48.     {
  49.         $page $event->getPage();
  50.         $product $page->getProduct();
  51.         $parentId $product->getParentId();
  52.         //If no parentId -> no variant, early return
  53.         if(!$parentId) {
  54.             return;
  55.         }
  56.         $defaultContext Context::createDefaultContext();
  57.         $criteria4Parent = new Criteria;
  58.         $criteria4Parent->addFilter(new EqualsFilter('id'$parentId));
  59.         $productSearchResult $this->standardProductRepository->search($criteria4Parent$defaultContext);
  60.         if ( $productSearchResult instanceof EntitySearchResult ) {
  61.             $productResults $productSearchResult->getElements();
  62.             if ( is_array($productResults) && count($productResults) == ) {
  63.                 $productParent current($productResults);
  64.             }
  65.         }
  66.         $sorting = new FieldSorting('productNumber''ASC');
  67.         $criteria = new Criteria();
  68.         $criteria->addFilter(new EqualsFilter('parentId'$parentId));
  69.         // $criteria->addAssociation('options.group');
  70.         $criteria->addAssociation('properties.group');
  71.         // $criteria->addAssociation('categories ');
  72.         // $criteria->addAssociation('media.cover');
  73.         $criteria->addSorting($sorting);
  74.         $variantProducts $this->productRepository->search($criteria$event->getSalesChannelContext());
  75.         //Only add extension when there is more than 1 variant
  76.         if($variantProducts->getTotal() > 1) {
  77.             $availableGroups = array();
  78.             $allVariantsProperties = array();
  79.             $productVariants $variantProducts->getEntities()->getElements();
  80.             if ( is_array($productVariants)) {
  81.                 foreach ($productVariants as $variant) {
  82.                     if ( is_object($variant) ) {
  83.                         if ( !isset($allVariantsProperties[$variant->getId()]) ) {
  84.                             $allVariantsProperties[$variant->getId()] = array('properties'=>array());
  85.                         }
  86.                         if ( $variant->getName() != '' ) {
  87.                             $allVariantsProperties[$variant->getId()]['name'] = $variant->getName();
  88.                         }
  89.                         else {
  90.                             $allVariantsProperties[$variant->getId()]['name'] = $productParent->getName();
  91.                         }
  92.                         $allVariantsProperties[$variant->getId()]['product_number'] = $variant->getProductNumber();
  93.                         $variantProperties $variant->getProperties();
  94.                         //var_dump($variantProperties);
  95.                         if ( is_object($variantProperties) ) {
  96.                             $variantPropertiesOptions $variantProperties->getElements();
  97.                             //$x = $variantProperties->getPosition();
  98.                             if ( is_array($variantPropertiesOptions) ) {
  99.                                 foreach ($variantPropertiesOptions as $propOpt) {
  100.                                     //echo $propOpt->getName()."<br>";
  101.                                     $propGroup $propOpt->getGroup();
  102.                                     //var_dump($propGroup);
  103.                                     //echo $propGroup->getPosition()."<br>";
  104.                                     if ( is_object($propGroup) ) {
  105.                                         if ( !isset($availableGroups[$propGroup->getId()]) ) {
  106.                                             $availableGroups[$propGroup->getId()] = array();
  107.                                         }
  108.                                         $availableGroups[$propGroup->getId()]['name'] = $propGroup->getName();
  109.                                         $availableGroups[$propGroup->getId()]['position'] = $propGroup->getPosition();
  110.                                         $allVariantsProperties[$variant->getId()]['properties'][$propGroup->getId()] = $propOpt->getName();
  111.                                     }
  112.                                 }
  113.                             }
  114.                         }
  115.                     }
  116.                 }
  117.             }
  118.             //echo '<div style="display:none">';
  119.             //print_r($availableGroups);
  120.             //print_r($allVariantsProperties);
  121.             //echo '</div>';
  122.             uasort($availableGroups, function($a$b){
  123.                 if ($a['position'] == $b['position']) {
  124.                     return 0;
  125.                 }
  126.                 return ($a['position'] < $b['position']) ? -1;
  127.             });
  128.             $ctnmFrontendHelperVariantsProperties = array(
  129.                 'table_header' => $availableGroups,
  130.                 'table_content' => $allVariantsProperties,
  131.             );
  132.             //print_r($productVariants->getElements());
  133.             $page->addExtension('CtnmFrontendHelperVariantsProperties', new ArrayEntity($ctnmFrontendHelperVariantsProperties));
  134.             $page->addExtension('CtnmFrontendHelperVariants'$variantProducts);
  135.         }
  136.     }
  137.     public function handleResult(ProductListingResultEvent $event)
  138.     {
  139.         $ids $event->getResult()->getEntities()->getIds();
  140.         $context Context::createDefaultContext();
  141.         if ( is_array($ids) ) {
  142.             foreach ($ids as $productId) {
  143.                 $product $event->getResult()->getEntities()->get($productId);
  144.                 if ( !$product instanceof SalesChannelProductEntity ) {
  145.                     continue;
  146.                 }
  147.                 //print_r($product);
  148.                 $productParentId $product->getParentId();
  149.                 if ( $productParentId ) {
  150.                     //$productParent = $event->getResult()->getEntities()->get($productParentId);
  151.                     $criteria = new Criteria;
  152.                     $criteria->addFilter(new EqualsFilter('id'$productParentId));
  153.                      $productSearchResult $this->standardProductRepository->search($criteria$context);
  154.                     if ( !$productSearchResult instanceof EntitySearchResult ) {
  155.                         continue;
  156.                     }
  157.                     $productResults $productSearchResult->getElements();
  158.                     if ( !is_array($productResults) ) {
  159.                         continue;
  160.                     }
  161.                     else {
  162.                         if ( count($productResults) != ) {
  163.                             continue;
  164.                         }
  165.                     }
  166.                     $productParent current($productResults);
  167.                     //echo "parent id: $productParentId<br>";
  168.                     /*echo "<pre>";
  169.                     print_r($productParent);
  170.                     echo "</pre>";*/
  171.                     if ( !$productParent instanceof ProductEntity ) {
  172.                         continue;
  173.                     }
  174.                     //echo $productParent->getName()."<br>";
  175.                     $extension = new TextStruct();
  176.                     $extension->setContent($productParent->getName());
  177.                     $product->addExtension('parentProductName'$extension);
  178.                 }
  179.             }
  180.         }
  181.         /*$resultEntities = $event->getResult()->getEntities();
  182.         $productsParentNames = array();
  183.         if ( is_object($resultEntities) ) {
  184.             $parentIds = $resultEntities->getParentIds();
  185.             if ( is_array($parentIds) ) {
  186.                 foreach ($parentIds as $productId => $parentId) {
  187.                     //echo $this->getParentName($event, $parentId)."<br>";
  188.                     $productsParentNames[$productId] = $this->getParentName($event, $parentId);
  189.                 }
  190.             }
  191.         }*/
  192.         //$page->addExtension('CtnmHeader', $header);
  193.     }
  194.     protected function getParentName(&$event$parentId)
  195.     {
  196.         $criteria = new Criteria();
  197.         $criteria->addFilter(new EqualsFilter('id'$parentId));
  198.         $criteria->addAssociation('properties.group');
  199.         $resultProducts $this->productRepository->search($criteria$event->getSalesChannelContext());
  200.         $resultSingleProduct $resultProducts->first();
  201.         if ( is_object($resultSingleProduct) ) {
  202.             return $resultSingleProduct->getName();
  203.         }
  204.         /*echo "<pre>";
  205.         print_r($resultProducts->first());
  206.         echo "</pre>";*/
  207.         return false;
  208.     }
  209.     public function onNavigationLoaded(NavigationPageLoadedEvent $event)
  210.     {
  211.         $page $event->getPage();
  212.         $header $page->getHeader();
  213.         $navigation $header->getNavigation();
  214.         $context Context::createDefaultContext();
  215.         $treeWith2Levels = array();
  216.         $treeWith2Levels2 = array();
  217.         //$tmpTreeArr = array();
  218.         if ( is_object($navigation) ) {
  219.             $catTree $navigation->getTree();
  220.             //var_dump($catTree);
  221.             $activeCategoryEntity $navigation->getActive();
  222.             $activeTreeItem $this->find($activeCategoryEntity->getId(), $catTree);
  223.             if ( is_object($activeTreeItem) ) {
  224.                 $treeItemChildren $activeTreeItem->getChildren();
  225.                 if ( is_array($treeItemChildren) ) {
  226.                     foreach ($treeItemChildren as $treeItemChild) {
  227.                         $childCategory $treeItemChild->getCategory();
  228.                         $treeItemChildChildren $treeItemChild->getChildren();
  229.                         if ( !isset($treeWith2Levels2[$childCategory->getId()]) ) {
  230.                             $treeWith2Levels2[$childCategory->getId()] = array();
  231.                         }
  232.                         $treeWith2Levels2[$childCategory->getId()]['name'] = $childCategory->getTranslation('name');
  233.                         //echo $childCategory->getName()."<br>";
  234.                         $treeWith2Levels2[$childCategory->getId()]['class'] = $childCategory;
  235.                         if ( isset($treeItemChildChildren) && is_array($treeItemChildChildren) && count($treeItemChildChildren) > 0) {
  236.                             /*if ( !isset($treeWith2Levels2[$childCategory->getId()]['children']) ) {
  237.                                 $treeWith2Levels2[$childCategory->getId()]['children'] = array();
  238.                             }*/
  239.                             $treeWith2Levels2[$childCategory->getId()]['children'] = $treeItemChildChildren;
  240.                             /*foreach ($treeItemChildChildren as $treeItemChild2) {
  241.                                 $childCategory2 = $treeItemChild2->getCategory();
  242.                                 //echo "<b>".$childCategory2->getName()."</b><br>";
  243.                             }*/
  244.                         }
  245.                     }
  246.                 }
  247.             }
  248.         }
  249.         //$page->addExtension('CtnmHeader', $navigation);
  250.         //$page->addExtension('CtnmHeader', new ArrayEntity($tmpTreeArr));
  251.         //$page->addExtension('CtnmHeader', new ArrayEntity($treeWith2Levels));
  252.         $page->addExtension('CtnmHeader', new ArrayEntity($treeWith2Levels2));
  253.         //$page->addExtension('CtnmHeader2', new ArrayEntity($treeWith2Levels2));
  254.         //$page->addExtension('CtnmHeader2', new ArrayEntity($cname));
  255.         /*$page->addExtension('CtnmHeader', new ArrayEntity(array(
  256.             'active' => $activeCategoryEntity,
  257.             'count' => $activeCategoryEntity->getChildCount(),
  258.             'children' => $activeCategoryEntity->getChildren(),
  259.         )));*/
  260.     }
  261.     private function find(string $categoryId, array $tree)
  262.     {
  263.         if (isset($tree[$categoryId])) {
  264.             return $tree[$categoryId];
  265.         }
  266.         foreach ($tree as $item) {
  267.             $nested $this->find($categoryId$item->getChildren());
  268.             if ($nested) {
  269.                 return $nested;
  270.             }
  271.         }
  272.         return null;
  273.     }
  274. }