custom/plugins/dkcProductoptions/src/Subscriber/ProductPageLoadedEventSubscriber.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Dkc\Productoptions\Subscriber;
  3. use Dkc\Productoptions\Entity\ProductOption\ProductOptionCollection;
  4. use Dkc\Productoptions\Struct\PriceStruct;
  5. use Dkc\Productoptions\Struct\LabelStruct;
  6. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\Context;
  12. class ProductPageLoadedEventSubscriber implements EventSubscriberInterface
  13. {
  14.     private EntityRepositoryInterface $productOptionRepository;
  15.     private EntityRepositoryInterface $productOptionProductRepository;
  16.     public function __construct(
  17.         EntityRepositoryInterface $productOptionRepository,
  18.         EntityRepositoryInterface $productOptionProductRepository
  19.     ) {
  20.         $this->productOptionRepository $productOptionRepository;
  21.         $this->productOptionProductRepository $productOptionProductRepository;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             ProductPageLoadedEvent::class => 'onProductPageLoaded'
  27.         ];
  28.     }
  29.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  30.     {
  31.         try {
  32.             $page $event->getPage();
  33.             $product $page->getProduct();
  34.             $productId $product->getId();
  35.             $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  36.             $context $event->getContext();
  37.             
  38.             // Kriterien erstellen
  39.             $criteria = new Criteria();
  40.             $criteria->addFilter(new EqualsFilter('products.id'$productId));
  41.             $criteria->addFilter(new EqualsFilter('salesChannels.id'$salesChannelId));
  42.             
  43.             // Assoziationen vollständig laden
  44.             $criteria->addAssociation('products');
  45.             
  46.             // Suche nach den Optionen
  47.             $options $this->productOptionRepository->search($criteria$context);
  48.             
  49.             // Preise aus der Zwischentabelle laden
  50.             $mappingCriteria = new Criteria();
  51.             $mappingCriteria->addFilter(new EqualsFilter('productId'$productId));
  52.             $mappings $this->productOptionProductRepository->search($mappingCriteria$context);
  53.             
  54.             $priceMappings = [];
  55.             foreach ($mappings as $mapping) {
  56.                 $optionId $mapping->get('productOptionId');
  57.                 $price $mapping->get('price');
  58.                 if ($optionId && $price !== null) {
  59.                     $priceMappings[$optionId] = $price;
  60.                 }
  61.             }
  62.             
  63.             // Optionen Collection vorbereiten
  64.             $optionsCollection = new ProductOptionCollection();
  65.             
  66.             foreach ($options as $option) {
  67.                 $optionId $option->getId();
  68.                 
  69.                 // Preis setzen
  70.                 if (isset($priceMappings[$optionId])) {
  71.                     $priceValue = (float)$priceMappings[$optionId];
  72.                     $priceStruct = new PriceStruct($priceValue);
  73.                     $option->addExtension('price'$priceStruct);
  74.                 }
  75.                 
  76.                 // Das Label als Struct speichern
  77.                 $labelData $option->getLabel();
  78.                 // Stelle sicher, dass wir ein Array haben
  79.                 if (!is_array($labelData)) {
  80.                     $labelData = [];
  81.                 }
  82.                 // Setze den Namen als Fallback
  83.                 if (empty($labelData) || (!isset($labelData['default']) && !isset($labelData['de-DE']))) {
  84.                     $labelData['default'] = $option->getName();
  85.                 }
  86.                 
  87.                 $labelStruct = new LabelStruct($labelData);
  88.                 $option->addExtension('optionLabel'$labelStruct);
  89.                 
  90.                 $optionsCollection->add($option);
  91.             }
  92.             
  93.             // Füge die Optionen zum Produkt hinzu
  94.             $product->addExtension('dkcProductOptions'$optionsCollection);
  95.             
  96.         } catch (\Exception $e) {
  97.             // Stille Fehlerbehandlung
  98.         }
  99.     }
  100. }