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\DataAbstractionLayer\Search\Sorting\FieldSorting;
  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.             // Kriterien erstellen - mit Group-Association
  38.             $criteria = new Criteria();
  39.             $criteria->addFilter(new EqualsFilter('products.id'$productId));
  40.             $criteria->addFilter(new EqualsFilter('salesChannels.id'$salesChannelId));
  41.             $criteria->addAssociation('products');
  42.             $criteria->addAssociation('group');
  43.             $criteria->getAssociation('group')->addSorting(new FieldSorting('position'FieldSorting::ASCENDING));
  44.             // Suche nach den Optionen
  45.             $options $this->productOptionRepository->search($criteria$context);
  46.             // Preise aus der Zwischentabelle laden
  47.             $mappingCriteria = new Criteria();
  48.             $mappingCriteria->addFilter(new EqualsFilter('productId'$productId));
  49.             $mappings $this->productOptionProductRepository->search($mappingCriteria$context);
  50.             $priceMappings = [];
  51.             foreach ($mappings as $mapping) {
  52.                 $optionId $mapping->get('productOptionId');
  53.                 $price $mapping->get('price');
  54.                 if ($optionId && $price !== null) {
  55.                     $priceMappings[$optionId] = $price;
  56.                 }
  57.             }
  58.             // Optionen Collection vorbereiten
  59.             $optionsCollection = new ProductOptionCollection();
  60.             foreach ($options as $option) {
  61.                 $optionId $option->getId();
  62.                 // Preis setzen
  63.                 if (isset($priceMappings[$optionId])) {
  64.                     $priceValue = (float) $priceMappings[$optionId];
  65.                     $priceStruct = new PriceStruct($priceValue);
  66.                     $option->addExtension('price'$priceStruct);
  67.                 }
  68.                 // Das Label als Struct speichern
  69.                 $labelData $option->getLabel();
  70.                 if (!is_array($labelData)) {
  71.                     $labelData = [];
  72.                 }
  73.                 if (empty($labelData) || (!isset($labelData['default']) && !isset($labelData['de-DE']))) {
  74.                     $labelData['default'] = $option->getName();
  75.                 }
  76.                 $labelStruct = new LabelStruct($labelData);
  77.                 $option->addExtension('optionLabel'$labelStruct);
  78.                 $optionsCollection->add($option);
  79.             }
  80.             // Fuege die Optionen zum Produkt hinzu
  81.             $product->addExtension('dkcProductOptions'$optionsCollection);
  82.         } catch (\Exception $e) {
  83.             // Stille Fehlerbehandlung - im Produktivbetrieb ggf. loggen
  84.         }
  85.     }
  86. }