custom/static-plugins/GlobusSW6/src/Subscriber/KlimaSplitSubscriber.php line 54

Open in your IDE?
  1. <?php
  2. namespace GlobusSW6\Subscriber;
  3. use GlobusSW6\Service\IaneoBusinessExtension;
  4. use GlobusSW6\Service\KlimaSplit\KlimaSplitService;
  5. use Shopware\Core\Framework\Api\Context\SystemSource;
  6. use Psr\Log\LoggerInterface;
  7. use Shopware\Core\Content\Product\ProductEntity;
  8. use Shopware\Core\Framework\Adapter\Translation\Translator;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  10. use Shopware\Core\Framework\Uuid\Uuid;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  13. class KlimaSplitSubscriber implements EventSubscriberInterface
  14. {
  15.     /** @var KlimaSplitService */
  16.     private $klimaSplitService;
  17.     /** @var IaneoBusinessExtension */
  18.     private $ianeoBusinessExtensionService;
  19.     /** @var Translator */
  20.     private $translator;
  21.     /** @var LoggerInterface */
  22.     private $logger;
  23.     /**
  24.      * KlimaSplitSubscriber constructor.
  25.      * @param KlimaSplitService $klimaSplitService
  26.      * @param IaneoBusinessExtension $ianeoBusinessExtensionService
  27.      * @param Translator $translator
  28.      * @param LoggerInterface $logger
  29.      */
  30.     public function __construct(KlimaSplitService $klimaSplitServiceIaneoBusinessExtension $ianeoBusinessExtensionServiceTranslator $translatorLoggerInterface $logger)
  31.     {
  32.         $this->klimaSplitService $klimaSplitService;
  33.         $this->ianeoBusinessExtensionService $ianeoBusinessExtensionService;
  34.         $this->translator $translator;
  35.         $this->logger $logger;
  36.     }
  37.     public static function getSubscribedEvents()
  38.     {
  39.         return [
  40.             'product.search.result.loaded' => 'onProduct'
  41.         ];
  42.     }
  43.     // TODO: currently missing: controller for handling ajax 'if pop-up was read' missing, as we first need the JS in the storefront
  44.     public function onProduct(EntitySearchResultLoadedEvent $event){
  45.         // do not fire event in backend context
  46.         if ($event->getContext()->getSource() instanceof AdminApiSource) {
  47.             if ($event->getContext()->getSource()->isAdmin()) {
  48.                 return;
  49.             }
  50.         }
  51.         if ($event->getContext()->getSource() instanceof SystemSource) {
  52.             return;
  53.         }
  54.         $products $event->getResult()->getElements();
  55.         if(empty($products)){
  56.             return;
  57.         }
  58.         try {
  59.             $salesChannelId $event->getContext()->getSource()->getSalesChannelId();
  60.             $languageId '2fbb5fe2e29a4d70aa5854ce7ce3e20b';
  61.             $this->translator->injectSettings(
  62.                 $salesChannelId,
  63.                 $languageId,
  64.                 'de-DE',// TODO-NGS: currently hardcoded
  65.                 $event->getContext()
  66.             );
  67.         } catch (\Throwable $t) {
  68.             $this->logger->info('Unable to translate snippets for api calls using the following params: saleschannelId: ' $salesChannelId  ' ; languageId: ' $languageId ' ; context: ' json_encode($event->getContext())
  69.             );
  70.         }
  71.         $klimaSplitProducts $this->klimaSplitService->checkIsKlimaSplit($event->getResult()->getEntities());
  72.         // add klima flags to product extension
  73.         $this->ianeoBusinessExtensionService->addAttributes($event->getResult()->getEntities(), $klimaSplitProducts'isKlimaSplit');
  74.         $this->ianeoBusinessExtensionService->addAttributes($event->getResult()->getEntities(), $klimaSplitProducts'showKlimaSplitModal');
  75.         $klimaSplitModalContent = [
  76.             'p1' => $this->translator->trans("klimasplit.Modal.p1"),
  77.             'p2' => $this->translator->trans("klimasplit.Modal.p2"),
  78.             'p3' => $this->translator->trans("klimasplit.Modal.p3"),
  79.             'p4' => $this->translator->trans("klimasplit.Modal.p4"),
  80.             'p5' => $this->translator->trans("klimasplit.Modal.p5")
  81.         ];
  82.         /** @var ProductEntity $product */
  83.         foreach ($products as $product) {
  84.             $this->ianeoBusinessExtensionService->addSingleAttributeToProduct($productnull'klimaSplitModalContent');
  85.             if($product->getExtensions()['ianeoExtensions']['isKlimaSplit']){
  86.                 $this->ianeoBusinessExtensionService->addSingleAttributeToProduct($product$klimaSplitModalContent'klimaSplitModalContent');
  87.             }
  88.         }
  89.     }
  90. }