custom/static-plugins/GlobusSW6/src/Subscriber/TextilData/ProductTextilDataExtensionSubscriber.php line 52

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace GlobusSW6\Subscriber\TextilData;
  3. use GlobusSW6\Service\TextilApi\TextilApiService;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Core\Content\Product\ProductCollection;
  6. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  7. use Shopware\Core\Framework\Adapter\Translation\Translator;
  8. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use GlobusSW6\Service\IaneoBusinessExtension;
  12. class ProductTextilDataExtensionSubscriber implements EventSubscriberInterface
  13. {
  14.     /** @var LoggerInterface  */
  15.     private $logger;
  16.     /** @var IaneoBusinessExtension */
  17.     private $ianeoBusinessExtension;
  18.     /** @var TextilApiService */
  19.     private $textilApiService;
  20.     /** @var Translator */
  21.     private $translator;
  22.     /**
  23.      * ProductTextilDataExtensionSubscriber constructor.
  24.      * @param LoggerInterface $logger
  25.      * @param IaneoBusinessExtension $ianeoBusinessExtension
  26.      * @param TextilApiService $textilApiService
  27.      * @param Translator $translator
  28.      */
  29.     public function __construct(LoggerInterface $loggerIaneoBusinessExtension $ianeoBusinessExtensionTextilApiService $textilApiService,Translator $translator)
  30.     {
  31.         $this->logger $logger;
  32.         $this->ianeoBusinessExtension $ianeoBusinessExtension;
  33.         $this->textilApiService $textilApiService;
  34.         $this->translator $translator;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             'product.search.result.loaded' => 'onProductLoaded'
  40.         ];
  41.     }
  42.     public function onProductLoaded(EntitySearchResultLoadedEvent $event)
  43.     {
  44.         /** @var ProductCollection $products */
  45.         $products $event->getResult()->getEntities();
  46.         if ($products === null) {
  47.             return;
  48.         }
  49.         if ($event->getContext()->getSource() instanceOf SalesChannelApiSource) {
  50.             try {
  51.                 $salesChannelId $event->getContext()->getSource()->getSalesChannelId();
  52.                 $languageId '2fbb5fe2e29a4d70aa5854ce7ce3e20b';
  53.                 $this->translator->injectSettings(
  54.                     $salesChannelId,
  55.                     $languageId,
  56.                     'de-DE',// TODO-NGS: currently hardcoded
  57.                     $event->getContext()
  58.                 );
  59.             } catch (\Throwable $t) {
  60.                 $this->logger->info('Unable to translate snippets for api calls using the following params: saleschannelId: ' $salesChannelId  ' ; languageId: ' $languageId ' ; context: ' json_encode($event->getContext())
  61.                 );
  62.             }
  63.             $hinweisAnimalParts $this->translator->trans("textil.animalParts");
  64.             $containsAnimalParts false;
  65.             foreach ($products as $product) {
  66.                 /** @var SalesChannelProductEntity $product */
  67.                 try {
  68.                     $textildata $this->textilApiService->getTextilByProductId($product->getId(), $event->getContext());
  69.                     if ($textildata) {
  70.                         foreach ($textildata->getProductCompositionData() as $component)
  71.                         {
  72.                             if($component['other_animal_parts'] === '1')
  73.                             {
  74.                                 $containsAnimalParts true;
  75.                             }
  76.                         }
  77.                         if ($containsAnimalParts){
  78.                             $textildata->setHinweisContainsAnimalPartsFlag($containsAnimalParts);
  79.                             $textildata->setHinweisContainsAnimalParts($hinweisAnimalParts);
  80.                         }
  81.                         $this->ianeoBusinessExtension->addSingleAttributeToProduct($product$textildata'textilData');
  82.                     }
  83.                 } catch (\Exception $exception) {
  84.                     $this->logger->error(__CLASS__ ":" __FUNCTION__ ":" __LINE__ ":\$exception->getMessage(): " $exception->getMessage());
  85.                 }
  86.             }
  87.         }
  88.     }
  89. }