<?php
namespace EnetProductStoreSalesChannel\Subscriber;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductPageLoaded implements EventSubscriberInterface
{
/** @var ContainerInterface */
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public static function getSubscribedEvents()
{
return [
ProductPageLoadedEvent::class => 'onProductPageLoaded'
];
}
public function onProductPageLoaded(ProductPageLoadedEvent $event)
{
$page = $event->getPage();
$page->getProduct()->addExtension(
'enetProductSaleschannelStock', $this->getStock($event)
);
// dump($event); die();
// $criteria = new Criteria();
//// $criteria->addFilter(new EqualsFilter('productId', $productId));
//// $criteria->addFilter(new EqualsFilter('salesChannelId', $saleschannelId));
//
// /** @var EntityRepositoryInterface $repo */
// $repo = $this->container->get('enet_product_saleschannel_stock.repository');
// $entry = $repo->search($criteria, Context::createDefaultContext());
// $elements = $entry->getElements();
}
protected function getStock(ProductPageLoadedEvent $event)
{
$salesChannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
$productId = $event->getPage()->getProduct()->getId();
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('productId', $productId));
$criteria->addFilter(new EqualsFilter('salesChannelId', $salesChannelId));
$repo = $this->container->get('enet_product_saleschannel_stock.repository');
$entry = $repo->search($criteria, Context::createDefaultContext());
return $entry;
}
}