custom/static-plugins/GlobusSW6/src/Subscriber/Variants/VariantSubscriber.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace GlobusSW6\Subscriber\Variants;
  3. use GlobusSW6\Core\Content\Attributes\Product\ProductAttributesEntity;
  4. use GlobusSW6\Service\IaneoBusinessExtension;
  5. use Shopware\Core\Content\Product\ProductEntity;
  6. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  7. use Shopware\Core\Framework\Api\Context\SystemSource;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  9. use Shopware\Core\Framework\Struct\ArrayStruct;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class VariantSubscriber implements EventSubscriberInterface
  12. {
  13.     /** @var IaneoBusinessExtension */
  14.     private $ianeoBusinessExtensionService;
  15.     /**
  16.      * @param IaneoBusinessExtension $ianeoBusinessExtensionService
  17.      */
  18.     public function __construct(IaneoBusinessExtension $ianeoBusinessExtensionService)
  19.     {
  20.         $this->ianeoBusinessExtensionService $ianeoBusinessExtensionService;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             'product.search.result.loaded' => 'onProductLoaded'
  26.         ];
  27.     }
  28.     public function onProductLoaded(EntitySearchResultLoadedEvent $event)
  29.     {
  30.         // do not fire event in backend context
  31.         if ($event->getContext()->getSource() instanceof AdminApiSource) {
  32.             if ($event->getContext()->getSource()->isAdmin()) {
  33.                 return;
  34.             }
  35.         }
  36.         if ($event->getContext()->getSource() instanceof SystemSource) {
  37.             return;
  38.         }
  39.         $products $event->getResult()->getEntities();
  40.         $variantInfoArray = [];
  41.         /** @var ProductEntity $product */
  42.         foreach ($products as $product) {
  43.             /** @var ProductAttributesEntity $ianeoAttributes */
  44.             $ianeoAttributes $product->getExtension('ianeoAttributes');
  45.             if (!is_null($ianeoAttributes->getVariantsListingMode())) {
  46.                 $variantInfos = new ArrayStruct();
  47.                 $variantInfos->set('isListingAufgefaechert'$ianeoAttributes->getVariantsListingMode() === 'a');
  48.                 $variantInfos->set('isPdpDropdown'$ianeoAttributes->getVariantsPdpPropertyMode() === 'd');
  49.                 $variantInfos->set('isPdpBox'$ianeoAttributes->getVariantsPdpPropertyMode() === 'b');
  50.                 $variantInfos->set('isSerienArtikel'$ianeoAttributes->getVariantsSerienArtikelOrVariant() === 's');
  51.                 $variantInfoArray[$product->getId()] = $variantInfos;
  52.             } else {
  53.                 $variantInfoArray[$product->getId()] = null;
  54.             }
  55.         }
  56.         $this->ianeoBusinessExtensionService->addAttributes($products$variantInfoArray'variantInfos');
  57.     }
  58. }