vendor/crehler/mojebambino-engine/src/Subscriber/ProductStatusSubscriber.php line 40

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * @copyright 2021 Crehler Sp. z o. o. <https://crehler.com>
  4.  *
  5.  * @author    Jakub MedyƄski <jme@crehler.com>
  6.  * @support   Crehler <support@crehler.com>
  7.  * @created   13 sty 2021
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Crehler\MojeBambinoEngine\Subscriber;
  13. use Crehler\MojeBambinoEngine\Service\ProductStatusStorefrontService;
  14. use Shopware\Core\Content\Product\ProductEvents;
  15. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  17. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class ProductStatusSubscriber implements EventSubscriberInterface
  20. {
  21.     private ProductStatusStorefrontService $productStatusService;
  22.     public function __construct(ProductStatusStorefrontService $productStatusService)
  23.     {
  24.         $this->productStatusService $productStatusService;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductLoaded',
  30.             SalesChannelEntityLoadedEvent::class => 'onProductLoaded'
  31.         ];
  32.     }
  33.     public function onProductLoaded(EntityLoadedEvent $event): void
  34.     {
  35.         try {
  36.             $products $event->getEntities();
  37.             /** @var SalesChannelProductEntity $product */
  38.             foreach ($products as $product) {
  39.                 if ($product instanceof SalesChannelProductEntity && !$product->hasExtension('productStatus')) {
  40.                     $product->addExtension('productStatus'$this->productStatusService->getProductStatus($product$event->getContext()));
  41.                 }
  42.             }
  43.         } catch (\Throwable $exception) {
  44.             dd($exception);
  45.         }
  46.     }
  47. }