custom/plugins/DmSanettaTheme/src/Subscriber/ProductHover.php line 40

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SanettaTheme\Subscriber;
  3. use Monolog\Logger;
  4. use SanettaTheme\Service\VariantLoader;
  5. use SanettaTheme\Storefront\Event\ProductHoverLoadedEvent;
  6. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  7. use Shopware\Core\Content\Product\Events\ProductSearchResultEvent;
  8. use Shopware\Core\Content\Product\ProductCollection;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Throwable;
  11. class ProductHover implements EventSubscriberInterface
  12. {
  13.     protected VariantLoader $variantLoader;
  14.     protected Logger $logger;
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             ProductListingResultEvent::class => [
  19.                 ['handleProductListingLoadedRequest'201],
  20.             ],
  21.             ProductSearchResultEvent::class => [
  22.                 ['handleProductListingLoadedRequest'201],
  23.             ],
  24.             ProductHoverLoadedEvent::class => [
  25.                 ['handleProductBoxLoadedRequest'201],
  26.             ],
  27.         ];
  28.     }
  29.     public function __construct(VariantLoader $loaderLogger $logger)
  30.     {
  31.         $this->variantLoader $loader;
  32.         $this->logger $logger;
  33.     }
  34.     public function handleProductListingLoadedRequest(ProductListingResultEvent $event): void
  35.     {
  36.         $products $event->getResult()->getEntities();
  37.         try {
  38.             $this->variantLoader->load($products$event->getSalesChannelContext());
  39.         } catch (Throwable $e) {
  40.             $this->logger->error(
  41.                 'Could not load variants.',
  42.                 [
  43.                     'productIds' => $products->getIds(),
  44.                     'throwable' => [
  45.                         'class'   => get_class($e),
  46.                         'message' => $e->getMessage(),
  47.                         'code'    => $e->getCode(),
  48.                     ],
  49.                 ]
  50.             );
  51.         }
  52.     }
  53.     public function handleProductBoxLoadedRequest(ProductHoverLoadedEvent $event): void
  54.     {
  55.         $product $event->getProduct();
  56.         try {
  57.             $this->variantLoader->load(
  58.                 new ProductCollection([$product]),
  59.                 $event->getSalesChannelContext()
  60.             );
  61.         } catch (Throwable $e) {
  62.             $this->logger->error(
  63.                 'Could not load variants.',
  64.                 [
  65.                     'productIds' => $product->getId(),
  66.                     'throwable' => [
  67.                         'class'   => get_class($e),
  68.                         'message' => $e->getMessage(),
  69.                         'code'    => $e->getCode(),
  70.                     ],
  71.                 ]
  72.             );
  73.         }
  74.     }
  75. }