custom/static-plugins/GlobusSW6/src/Subscriber/ArticleVisibility/ArticleVisibilitySubscriber.php line 132

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace GlobusSW6\Subscriber\ArticleVisibility;
  3. use GlobusSW6\Core\IaneoDefaults;
  4. use GlobusSW6\Service\ArticleVisibilityService;
  5. use GlobusSW6\Service\ChangeVisibilityService;
  6. use Shopware\Core\Content\Product\ProductEntity;
  7. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  8. use Shopware\Core\Framework\Api\Context\SystemSource;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  14. use Shopware\Core\Content\Product\ProductCollection;
  15. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  16. use Psr\Log\LoggerInterface;
  17. class ArticleVisibilitySubscriber implements EventSubscriberInterface
  18. {
  19.     /** @var ArticleVisibilityService */
  20.     private $articleVisibilityService;
  21.     /** @var ChangeVisibilityService */
  22.     private $changeVisibilityService;
  23.     /** @var LoggerInterface */
  24.     private $logger;
  25.     /**
  26.      * ArticleVisibilitySubscriber constructor.
  27.      * @param ArticleVisibilityService $articleVisibilityService
  28.      * @param ChangeVisibilityService $changeVisibilityService
  29.      * @param LoggerInterface $logger
  30.      */
  31.     public function __construct(
  32.         ArticleVisibilityService $articleVisibilityService,
  33.         ChangeVisibilityService $changeVisibilityService,
  34.         LoggerInterface $logger
  35.     ) {
  36.         $this->articleVisibilityService $articleVisibilityService;
  37.         $this->changeVisibilityService $changeVisibilityService;
  38.         $this->logger $logger;
  39.     }
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             'product.search.result.loaded' => 'onProductLoaded',
  44.             'product.written' => 'onProductWritten',
  45.         ];
  46.     }
  47.     /**
  48.      * Nicht auf Admin beschränken, damit später auch Zuweisungen via API möglich sind.
  49.      * Automatisiertes Zuweisen von SalesChannels anhand der zugewiesenen Kategorie
  50.      *
  51.      * @param EntityWrittenEvent $entityWrittenEvent
  52.      */
  53.     public function onProductWritten(EntityWrittenEvent $entityWrittenEvent): void
  54.     {
  55.         //TODO: Fälle unterscheiden Storefront oder Admin Event
  56.         // SalesChannelId in AdminSource nicht verfügbar --> catch-Fall tritt direkt ein
  57.         $context $entityWrittenEvent->getContext();
  58.        try {
  59.             $salesChannelId $entityWrittenEvent->getContext()->getSource()->getSalesChannelId();
  60.         } catch (\Throwable $t) {
  61.             $this->logger->info('Unable to access salesChannelId');
  62.             return;
  63.         }
  64.         /** @var EntityWriteResult $writeResults */
  65.         foreach ($entityWrittenEvent->getWriteResults() as $writeResult) {
  66.             $productId $writeResult->getPrimaryKey();
  67.             if (!$context->hasExtension('ianeoArticleApi')) {
  68.                 // Visibility - Kategorie- SalesChanel-Zuweisung
  69.                 $this->articleVisibilityService->handleArticleVisibilityBySelectedCategory($productId);
  70.                 // visibility ändern falls notwendig
  71.                 $productEntities $this->articleVisibilityService->getProductCollectionByProductIds(
  72.                     $productId,
  73.                     $entityWrittenEvent->getContext()
  74.                 );
  75.                 $visibilities $this->articleVisibilityService->handleArticleVisibility($productEntities$salesChannelId);
  76.                 $invisibleProductIds array_keys($visibilities);
  77.                 $productHasNoStock false;
  78.                 $productIdWithNoStock '';
  79.                 foreach($invisibleProductIds as $invisibleProductId){
  80.                     if( isset($visibilities[$invisibleProductId])){
  81.                         foreach($visibilities[$invisibleProductId] as $visibility){
  82.                             //TODO-INI: currently hardcoded
  83.                             if($visibility === 'Das Produkt hat keinen Bestand.'){
  84.                                 $productHasNoStock true;
  85.                                 $productIdWithNoStock $invisibleProductId;
  86.                             }
  87.                         }
  88.                     }
  89.                 }
  90.                 if (!in_array($productId$invisibleProductIds)) {
  91.                     $this->changeVisibilityService->setVisibilityTo30(
  92.                         $productId,
  93.                         $salesChannelId
  94.                     );
  95.                 } else {
  96.                     if($productHasNoStock && $productId === $productIdWithNoStock){
  97.                         $this->changeVisibilityService->setVisibilityTo10(
  98.                             $productId,
  99.                             $salesChannelId
  100.                         );
  101.                     }else{
  102.                         $this->changeVisibilityService->setVisibilityToZero(
  103.                             [$productId],
  104.                             $salesChannelId
  105.                         );
  106.                     }
  107.                 }
  108.             }
  109.         }
  110.     }
  111.     public function onProductLoaded(EntitySearchResultLoadedEvent $event)
  112.     {
  113.         // do not fire event in backend context
  114.         if ($event->getContext()->getSource() instanceof AdminApiSource) {
  115.             if ($event->getContext()->getSource()->isAdmin()) {
  116.                 return;
  117.             }
  118.         }
  119.         if ($event->getContext()->getSource() instanceof SystemSource) {
  120.             return;
  121.         }
  122.         $salesChannelId $event->getContext()->getSource()->getSalesChannelId();
  123.         /** @var ProductCollection $products */
  124.         $products $event->getResult()->getEntities();
  125.         if ($products === null || !($event->getContext()->getSource() instanceof SalesChannelApiSource)) {
  126.             return;
  127.         }
  128.         foreach ($products as $product) {
  129.             try {
  130. //                  $this->logger->info('Product attributes of ' . $product->getName() . ' ' . $product->getProductNumber() . ' : ' . json_encode($product->getExtensions()['ianeoAttributes']));
  131. //                  $this->logger->info('Product extension of ' . $product->getName() . ' ' . $product->getProductNumber() . ' : ' . json_encode($product->getExtensions()['ianeoExtensions']));
  132.             } catch (\Throwable $t) {
  133.             }
  134.         }
  135.         // collect visibilities for each product
  136.         $visibilities $this->articleVisibilityService->handleArticleVisibility($products$salesChannelId);
  137.         $invisibleProductIds array_keys($visibilities);
  138.         $salesChannelId $event->getContext()->getSource()->getSalesChannelId();
  139.         // ProductHasNoStock
  140.         $productHasNoStock false;
  141.         $productIdWithNoStock '';
  142.         foreach($invisibleProductIds as $invisibleProductId){
  143.             if( isset($visibilities[$invisibleProductId])){
  144.                 foreach($visibilities[$invisibleProductId] as $visibility){
  145.                     //TODO-INI: currently hardcoded
  146.                     if($visibility === 'Das Produkt hat keinen Bestand.'){
  147.                         $productHasNoStock true;
  148.                         $productIdWithNoStock $invisibleProductId;
  149.                     }
  150.                 }
  151.             }
  152.         }
  153.         if($productHasNoStock){
  154.             $this->changeVisibilityService->setVisibilityTo10(
  155.                $invisibleProductId,
  156.                 $salesChannelId
  157.             );
  158.         }else{
  159.             // visibility is set to 0 for future search
  160.             $this->changeVisibilityService->setVisibilityToZero(
  161.                 $invisibleProductIds,
  162.                 $salesChannelId);
  163.         }
  164.         // also remove these products from current search result
  165.         foreach ($invisibleProductIds as $productId) {
  166.             $products->remove($productId);
  167.         }
  168.     }
  169. }