<?php declare(strict_types=1);
namespace DvdwDirectory\Content\Product\Subscriber;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductPageLoadedSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'onProductPage',
];
}
public function onProductPage(ProductPageLoadedEvent $event): void
{
$page = $event->getPage();
$metaInformation = $page->getMetaInformation();
if ((string) $page->getProduct()->getTranslation('metaTitle') === '') {
$metaTitleParts = [$page->getProduct()->getTranslation('name')];
foreach ($page->getSelectedOptions() as $option) {
$metaTitleParts[] = $option->getTranslation('name');
}
$metaTitleParts[] = $event->getSalesChannelContext()->getSalesChannel()->getName();
$metaInformation->setMetaTitle(implode(' | ', $metaTitleParts));
}
}
}