<?php declare(strict_types=1);
namespace TemplaidRecommendations\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use TemplaidRecommendations\Core\Content\Product\RecommendationDefinition;
use TemplaidRecommendations\Service\RecommendationService;
class ProductSubscriber implements EventSubscriberInterface
{
private RecommendationService $recommendationService;
public function __construct(
RecommendationService $recommendationService
)
{
$this->recommendationService = $recommendationService;
}
/**
* @return array|string[]
*/
public static function getSubscribedEvents()
{
return [
ProductPageLoadedEvent::class => 'onProductPageLoaded'
];
}
/**
* @param ProductPageLoadedEvent $event
*/
public function onProductPageLoaded(ProductPageLoadedEvent $event): void
{
if (!$event->getSalesChannelContext()->getCustomer()) {
return;
}
$data = [[
'productId' => $event->getPage()->getProduct()->getId(),
'customerId' => $event->getSalesChannelContext()->getCustomerId(),
'type' => RecommendationDefinition::TYPE_LAST_VIEWED_BY_CUSTOMER
]];
$this->recommendationService->createRecommendations($data, $event->getSalesChannelContext()->getContext());
}
}