custom/plugins/EnetProductStoreSalesChannel/src/Subscriber/ProductPageLoaded.php line 29

Open in your IDE?
  1. <?php
  2. namespace EnetProductStoreSalesChannel\Subscriber;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  6. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class ProductPageLoaded implements EventSubscriberInterface
  10. {
  11.     /** @var ContainerInterface */
  12.     protected $container;
  13.     public function __construct(ContainerInterface $container)
  14.     {
  15.         $this->container $container;
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return [
  20.             ProductPageLoadedEvent::class => 'onProductPageLoaded'
  21.         ];
  22.     }
  23.     public function onProductPageLoaded(ProductPageLoadedEvent $event)
  24.     {
  25.         $page $event->getPage();
  26.         $page->getProduct()->addExtension(
  27.             'enetProductSaleschannelStock'$this->getStock($event)
  28.         );
  29. //        dump($event); die();
  30. //        $criteria = new Criteria();
  31. ////        $criteria->addFilter(new EqualsFilter('productId', $productId));
  32. ////        $criteria->addFilter(new EqualsFilter('salesChannelId', $saleschannelId));
  33. //
  34. //        /** @var EntityRepositoryInterface $repo */
  35. //        $repo = $this->container->get('enet_product_saleschannel_stock.repository');
  36. //        $entry = $repo->search($criteria, Context::createDefaultContext());
  37. //        $elements = $entry->getElements();
  38.     }
  39.     protected function getStock(ProductPageLoadedEvent $event)
  40.     {
  41.         $salesChannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  42.         $productId $event->getPage()->getProduct()->getId();
  43.         $criteria = new Criteria();
  44.         $criteria->addFilter(new EqualsFilter('productId'$productId));
  45.         $criteria->addFilter(new EqualsFilter('salesChannelId'$salesChannelId));
  46.         $repo $this->container->get('enet_product_saleschannel_stock.repository');
  47.         $entry $repo->search($criteriaContext::createDefaultContext());
  48.         return $entry;
  49.     }
  50. }