custom/plugins/MetalementsTheme/src/Subscriber/ProductVariantenMedia.php line 118

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace MetalementsTheme\Subscriber;
  4. use Shopware\Core\Framework\Struct\ArrayStruct;
  5. use Shopware\Core\Framework\Struct\ArrayEntity;
  6. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\OrFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Content\Media\DataAbstractionLayer\MediaRepositoryDecorator;
  13. class ProductVariantenMedia implements EventSubscriberInterface
  14. {
  15.     private $productRepository;
  16.     private MediaRepositoryDecorator $mediaRepository;
  17.     public function __construct(EntityRepositoryInterface $productRepositoryMediaRepositoryDecorator $mediaRepository)
  18.     {
  19.         $this->productRepository $productRepository;
  20.         $this->mediaRepository $mediaRepository;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             ProductPageLoadedEvent::class => 'onProductPageLoaded'
  26.         ];
  27.     }
  28.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  29.     {
  30.         $product  $event->getPage()->getProduct();
  31.         $parentProduct $product->getParent();
  32.         if ($parentProduct !== null) {
  33.             $product $parentProduct;
  34.         }
  35.         $criteria = new Criteria();
  36.         $criteria->addFilter(new EqualsFilter(
  37.             'parentId',
  38.             $product->getParentId()
  39.         ));
  40.         $criteria->addAssociation('cover.media');
  41.         $criteria->addAssociation('options.group');
  42.         // retrieve all product variants
  43.         $products $this->productRepository->search($criteria$event->getContext());
  44.         $matchingVariants $products->getEntities()->getElements();
  45.         $productColor null;
  46.         $debug = [];
  47.         $options_value null;
  48.         $allOptions = [];
  49.         // find which options are selected, i.e. which 'Eigenschaften' does the current product have.
  50.         $NummberOfOptions 0;
  51.         foreach ($product->getOptions() as  $option) {
  52.             if (strpos($option->getGroup()->name'Design') == false) {
  53.                 $options_value[] = $option->name;
  54.                 $NummberOfOptions $NummberOfOptions 1;
  55.             }
  56.  
  57.         }
  58.         //die('<pre>' . print_r($NummberOfOptions, true) . '</pre>');
  59.         // echo "<pre>";
  60.         // print_r($debug);
  61.         // die();
  62.         // break;
  63.         $matchingVariants $products->getEntities()->getElements();
  64.         $filteredVariants null;
  65.         // then filter variants based on the selected 'Eigenschaften'
  66.         $NummberOfOptionsThatMatch 0;
  67.         foreach ($matchingVariants as $variantProduct) {
  68.             $NummberOfOptionsThatMatch 0;
  69.             $allOptions[] = 'Product:';
  70.             foreach ($variantProduct->getOptions() as  $option) {
  71.                 if(in_array($option->name$options_value)) {
  72.                     $NummberOfOptionsThatMatch $NummberOfOptionsThatMatch 1;
  73.                 }
  74.                 $allOptions[] = $option->name;
  75.             }
  76.             if ($NummberOfOptionsThatMatch == $NummberOfOptions) {
  77.                 $filteredVariants[] = $variantProduct;
  78.             }
  79.         }
  80.         foreach ($filteredVariants as $variant) {
  81.             $coverId $variant->getCover()->getMediaId();
  82.             if ($coverId) {
  83.                 $mediaCriteria = new Criteria();
  84.                 $mediaCriteria->addFilter(new EqualsFilter('id'$coverId));
  85.                 $mediaEntity $this->mediaRepository->search($mediaCriteria$event->getContext())->first();
  86.                 if ($mediaEntity) {
  87.                     $mediaEntity->addExtension('debugVariant'$variant);
  88.                     $variant->addExtension('variantMedia'$mediaEntity);
  89.                 }
  90.             }
  91.         }
  92.         $array = ['wct_product_varianten' => $filteredVariants];
  93.         $event->getPage()->assign($array);
  94.         $event->getPage()->assign(['all_options' => $allOptions]);
  95.     }
  96. }