custom/static-plugins/GlobusSW6/src/Subscriber/Psa/ProductPsaExtensionSubscriber.php line 47

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace GlobusSW6\Subscriber\Psa;
  3. use Shopware\Core\Content\Product\ProductCollection;
  4. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use GlobusSW6\Service\PsaApi\PsaApiService;
  8. use Psr\Log\LoggerInterface;
  9. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  10. use GlobusSW6\Service\IaneoBusinessExtension;
  11. class ProductPsaExtensionSubscriber implements EventSubscriberInterface
  12. {
  13.     /** @var LoggerInterface  */
  14.     private $logger;
  15.     /** @var IaneoBusinessExtension */
  16.     private $ianeoBusinessExtension;
  17.     /** @var PsaApiService */
  18.     private $psaApiService;
  19.     /**
  20.      * ProductPsaExtensionSubscriber constructor.
  21.      * @param LoggerInterface $logger
  22.      * @param IaneoBusinessExtension $ianeoBusinessExtension
  23.      * @param PsaApiService $psaApiService
  24.      */
  25.     public function __construct(LoggerInterface $loggerIaneoBusinessExtension $ianeoBusinessExtensionPsaApiService $psaApiService)
  26.     {
  27.         $this->logger $logger;
  28.         $this->ianeoBusinessExtension $ianeoBusinessExtension;
  29.         $this->psaApiService $psaApiService;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             'product.search.result.loaded' => 'onProductLoaded'
  35.         ];
  36.     }
  37.     public function onProductLoaded(EntitySearchResultLoadedEvent $event)
  38.     {
  39.         /** @var ProductCollection $products */
  40.         $products $event->getResult()->getEntities();
  41.         if ($products === null) {
  42.             return;
  43.         }
  44.         if ($event->getContext()->getSource() instanceOf SalesChannelApiSource)
  45.         {
  46.             foreach ($products as $product) {
  47.                 try {
  48.                     $psa $this->psaApiService->getPsaByProductId($product->getId(), $event->getContext());
  49.                     if($psa) {
  50.                         $this->ianeoBusinessExtension->addSingleAttributeToProduct($product$psa'psa');
  51.                     }
  52.                 } catch(\Exception $exception) {
  53.                     $this->logger->error(__CLASS__ ":" __FUNCTION__ ":" __LINE__ ":\$exception->getMessage(): " $exception->getMessage());
  54.                 }
  55.             }
  56.         }
  57.     }
  58. }