custom/static-plugins/NdFashion/src/Subscriber/AddChildrenToProductPage.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nd\Fashion\Subscriber;
  3. use DateTime;
  4. use Nd\Fashion\Service\ProductManufacturerUrlService;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Content\Product\Aggregate\ProductMedia\ProductMediaEntity;
  7. use Shopware\Core\Content\Product\ProductEntity;
  8. use Shopware\Core\Content\Product\SalesChannel\Detail\ProductConfiguratorLoader;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\Struct\ArrayStruct;
  13. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  14. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  15. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Shopware\Core\Content\Product\ProductEvents;
  18. class AddChildrenToProductPage implements EventSubscriberInterface
  19. {
  20.     private SalesChannelRepositoryInterface $productRepository;
  21.     private ProductManufacturerUrlService $productManufacturerUrlService;
  22.     public function __construct(SalesChannelRepositoryInterface $productRepository,
  23.                                 ProductManufacturerUrlService   $productManufacturerUrlService)
  24.     {
  25.         $this->productRepository $productRepository;
  26.         $this->productManufacturerUrlService $productManufacturerUrlService;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             ProductPageLoadedEvent::class => 'onProductPageLoaded'
  32.         ];
  33.     }
  34.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  35.     {
  36.         $product $event->getPage()->getProduct();
  37.         $context $event->getSalesChannelContext();
  38.         if ($context->hasState('elasticsearchAware')) {
  39.             $context->removeState('elasticsearchAware');
  40.         }
  41.         if ($product->getParentId() == null && $product->getChildCount() > && $product->getChildren() == null) {
  42.             $criteria = new Criteria();
  43.             $criteria->addFilter(new EqualsFilter('id'$product->getId()));
  44.             $criteria->addAssociation("configuratorSettings.option.group");
  45.             $productsWithConfiguratorSettings $this->productRepository->search($criteria$context);
  46.             if ($productsWithConfiguratorSettings->count() != 1) {
  47.                 return;
  48.             }
  49.             $productWithConfiguratorSettings $productsWithConfiguratorSettings->first();
  50.             $configuratorSettings $productWithConfiguratorSettings->getConfiguratorSettings();
  51.             $optionSorting = [];
  52.             foreach ($configuratorSettings as $configuratorSetting) {
  53.                 while (in_array($configuratorSetting->getPosition(), array_keys($optionSorting))) {
  54.                     $configuratorSetting->setPosition($configuratorSetting->getPosition() + 1);
  55.                 }
  56.                 $optionSorting[$configuratorSetting->getPosition()] = $configuratorSetting->getOptionId();
  57.             }
  58.             ksort($optionSorting);
  59.             $criteria = new Criteria();
  60.             $criteria->addFilter(new EqualsFilter('parentId'$product->getId()));
  61.             $criteria->addAssociation("options");
  62.             $children $this->productRepository->search($criteria$context);
  63.             $optionSortingFlipped array_flip($optionSorting);
  64.             $productSorting = [];
  65.             foreach ($children as $child) {
  66.                 if (!$child->getOptionIds()) {
  67.                     continue;
  68.                 }
  69.                 $id $child->getOptionIds()[0];
  70.                 if (!$id || !in_array($idarray_keys($optionSortingFlipped))) {
  71.                     continue;
  72.                 }
  73.                 $productSorting[$optionSortingFlipped[$id]] = $child->getId();
  74.             }
  75.             ksort($productSorting);
  76.             if (count($productSorting) > 0) {
  77.                 $children->getEntities()->sortByIdArray($productSorting);
  78.             }
  79.             $product->setChildren($children->getEntities());
  80.             $event->getPage()->setProduct($product);
  81.         }
  82.         if ($product->getManufacturer() && !in_array("url"$product->getManufacturer()->getExtensions())) {
  83.             $manufacturer $product->getManufacturer();
  84.             $url $this->productManufacturerUrlService->retrieveUrl($manufacturer$context);
  85.             $manufacturer->addExtension("url", new ArrayStruct([$url]));
  86.         }
  87.     }
  88. }