<?php
declare(strict_types=1);
namespace Bodymed\Webshop\Core\Subscriber;
use Bodymed\Webshop\Constants;
use Bodymed\Webshop\Core\Content\CustomerProductFeature\CustomerProductFeatureEntity;
use Shopware\Core\Content\Product\ProductCollection;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Shopware\Core\PlatformRequest;
class ProductSubscriber implements EventSubscriberInterface
{
private RequestStack $requestStack;
private SalesChannelContextPersister $salesChannelContextPersister;
private EntityRepositoryInterface $customerProductFeatureRepository;
public function __construct(
RequestStack $requestStack,
SalesChannelContextPersister $salesChannelContextPersister,
EntityRepositoryInterface $customerProductFeatureRepository
) {
$this->requestStack = $requestStack;
$this->salesChannelContextPersister = $salesChannelContextPersister;
$this->customerProductFeatureRepository = $customerProductFeatureRepository;
}
public static function getSubscribedEvents(): array
{
return [
ProductEvents::PRODUCT_LOADED_EVENT => 'addCustomerProductFeatureExtension'
];
}
public function addCustomerProductFeatureExtension(EntityLoadedEvent $event): void
{
$context = $event->getContext();
if ($event->getDefinition() instanceof ProductDefinition) {
/* @var $products ProductCollection */
$products = $event->getEntities();
$source = $event->getContext()->getSource();
if (null === $this->requestStack->getCurrentRequest()) {
return;
}
if ($source instanceof SalesChannelApiSource) {
$token = $this->requestStack->getCurrentRequest()->headers->get(PlatformRequest::HEADER_CONTEXT_TOKEN);
$salesChannelId = $source->getSalesChannelId();
if($salesChannelId and $token){
$payload = $this->salesChannelContextPersister->load($token, $salesChannelId);
if (isset($payload['customerId'])) {
$criteria = (new Criteria())->addFilter(
new EqualsFilter('customerId', $payload['customerId']),
new EqualsAnyFilter('productId', $event->getIds())
);
$result = $this->customerProductFeatureRepository->search($criteria, $context);
foreach ($products as $product) {
/* @var $customerProductFeature CustomerProductFeatureEntity */
$result = $result->filterByProperty(
'productId',
$product->getId()
)->filterByProperty('customerId', $payload['customerId']);
$customerProductFeature = $result->first();
if ($customerProductFeature instanceof CustomerProductFeatureEntity) {
$product->addExtension(
Constants::CUSTOMER_PRODUCT_FEATURE_EXTENSION_KEY,
$customerProductFeature
);
}
}
}
}
}
}
}
}