<?php
declare(strict_types=1);
namespace MetalementsTheme\Subscriber;
use Shopware\Core\Content\Product\ProductPage\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\Product\ProductPage;
class ProductPageRedirectSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
ProductPageLoadedEvent::class => 'onProductPageLoaded',
];
}
public function onProductPageLoaded(ProductPageLoadedEvent $event)
{
/** @var ProductPage $productPage */
$productPage = $event->getPage();
$product = $productPage->getProduct();
// Hier die Kategorie-ID oder UUID der Kategorie für die Umleitung setzen
$targetCategoryId = '5df62053aa5b43528ce9c4f91f65249b';
if ($product->getCategories()->has($targetCategoryId)) {
// Umleitung zur Startseite
$event->getResponse()->headers->set('Location', '/');
$event->getResponse()->setStatusCode(302);
$event->stopPropagation();
}
}
}