custom/plugins/EsmComputer/src/Storefront/Subscriber/HideOutOfStockProductsSubscriber.php line 66

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace EsmComputer\Storefront\Subscriber;
  3. use EsmComputer\Core\Content\Product\OutOfStockService;
  4. use Shopware\Core\Content\Product\Events\ProductCrossSellingIdsCriteriaEvent;
  5. use Shopware\Core\Content\Product\Events\ProductCrossSellingStreamCriteriaEvent;
  6. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  7. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  8. use Shopware\Core\Content\Product\Events\ProductSuggestCriteriaEvent;
  9. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12. class HideOutOfStockProductsSubscriber implements EventSubscriberInterface
  13. {
  14.     protected OutOfStockService $outOfStockService;
  15.     public function __construct(
  16.         OutOfStockService $outOfStockService
  17.     )
  18.     {
  19.         $this->outOfStockService $outOfStockService;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             ProductListingCriteriaEvent::class => [
  25.                 ['handleListingRequest'200]
  26.             ],
  27.             ProductSuggestCriteriaEvent::class => [
  28.                 ['handleSearchRequest'200]
  29.             ],
  30.             ProductSearchCriteriaEvent::class => [
  31.                 ['handleSearchRequest'200]
  32.             ],
  33.             ProductCrossSellingIdsCriteriaEvent::class => [
  34.                 ['handleCrossSellingProductListingRequest'200]
  35.             ],
  36.             ProductCrossSellingStreamCriteriaEvent::class => [
  37.                 ['handleCrossSellingProductStreamRequest'200]
  38.             ],
  39.             ProductPageLoadedEvent::class => [
  40.                 ['onProductLoaded'50]
  41.             ]
  42.         ];
  43.     }
  44.     public function handleCrossSellingProductStreamRequest(ProductCrossSellingStreamCriteriaEvent $event): void
  45.     {
  46.         $this->outOfStockService->addAvailableCriteria($event->getCriteria());
  47.     }
  48.     public function handleCrossSellingProductListingRequest(ProductCrossSellingIdsCriteriaEvent $event): void
  49.     {
  50.         $this->outOfStockService->addAvailableCriteria($event->getCriteria());
  51.     }
  52.     public function handleListingRequest(ProductListingCriteriaEvent $event): void
  53.     {
  54.         // Not needed, out of stock products are not visible anyway
  55.         // $this->outOfStockService->addAvailableCriteria($event->getCriteria());
  56.     }
  57.     public function handleSearchRequest(ProductListingCriteriaEvent $event): void
  58.     {
  59.         $this->outOfStockService->addAvailableCriteria($event->getCriteria());
  60.     }
  61.     public function onProductLoaded(ProductPageLoadedEvent $event): void
  62.     {
  63.         $product $event->getPage()->getProduct();
  64.         if(empty($product->getId())) {
  65.             return;
  66.         }
  67.         if($this->outOfStockService->isOutOfStock($product$event->getSalesChannelContext())) {
  68.             throw new NotFoundHttpException();
  69.         }
  70.     }
  71. }