custom/static-plugins/GlobusSW6/src/Subscriber/CheckoutVisibilitiesSubscriber.php line 46

Open in your IDE?
  1. <?php
  2. namespace GlobusSW6\Subscriber;
  3. use GlobusSW6\Service\CheckoutVisibility\CheckoutVisibilityManager;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Core\Content\Product\ProductCollection;
  6. use Shopware\Core\Framework\Adapter\Translation\Translator;
  7. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  8. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  9. use Shopware\Core\Framework\Api\Context\SystemSource;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class CheckoutVisibilitiesSubscriber implements EventSubscriberInterface
  13. {
  14.     /** @var CheckoutVisibilityManager */
  15.     private $checkoutVisibilityManager;
  16.     /** @var Translator */
  17.     private $translator;
  18.     /** @var LoggerInterface */
  19.     private $logger;
  20.     /**
  21.      * @param CheckoutVisibilityManager $checkoutVisibilityManager
  22.      * @param Translator $translator
  23.      * @param LoggerInterface $logger
  24.      */
  25.     public function __construct(CheckoutVisibilityManager $checkoutVisibilityManagerTranslator $translatorLoggerInterface $logger)
  26.     {
  27.         $this->checkoutVisibilityManager $checkoutVisibilityManager;
  28.         $this->translator $translator;
  29.         $this->logger $logger;
  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.         if ($event->getContext()->getSource() instanceof SystemSource) {
  40.             return;
  41.         }
  42.         try {
  43.             $this->translator->injectSettings(
  44.                 $event->getContext()->getSource()->getSalesChannelId(),
  45.                 $event->getContext()->getLanguageId(),
  46.                 'de-DE'// TODO-NGS: currently hardcoded
  47.                 $event->getContext()
  48.             );
  49.         } catch (\Throwable $t) {
  50.             $this->logger->info('Unable to translate snippets for api calls using the following params: saleschannelId: ' json_encode($event->getContext()->getSource())  . ' ; languageId: ' $event->getContext()->getLanguageId() . ' ; context: ' json_encode($event->getContext()) );
  51.         }
  52.         /** @var ProductCollection $products */
  53.         $products $event->getResult()->getEntities();
  54.         if ($products === null) {
  55.             return;
  56.         }
  57.         if ( $event->getContext()->getSource() instanceOf SalesChannelApiSource){
  58.             $salesChannelId $event->getContext()->getSource()->getSalesChannelId();
  59.             $this->checkoutVisibilityManager->handleVisibilities($products$salesChannelId$this->translator$event->getContext()->getLanguageId());
  60.         }
  61.     }
  62. }