custom/plugins/MetalementsTheme/src/Subscriber/ProductPageRedirectSubscriber.php line 20

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace MetalementsTheme\Subscriber;
  4. use Shopware\Core\Content\Product\ProductPage\ProductPageLoadedEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Storefront\Page\Product\ProductPage;
  7. class ProductPageRedirectSubscriber implements EventSubscriberInterface
  8. {
  9.     public static function getSubscribedEvents()
  10.     {
  11.         return [
  12.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  13.         ];
  14.     }
  15.     public function onProductPageLoaded(ProductPageLoadedEvent $event)
  16.     {
  17.         /** @var ProductPage $productPage */
  18.         $productPage $event->getPage();
  19.         $product $productPage->getProduct();
  20.         // Hier die Kategorie-ID oder UUID der Kategorie für die Umleitung setzen
  21.         $targetCategoryId '5df62053aa5b43528ce9c4f91f65249b';
  22.         if ($product->getCategories()->has($targetCategoryId)) {
  23.             // Umleitung zur Startseite
  24.             $event->getResponse()->headers->set('Location''/');
  25.             $event->getResponse()->setStatusCode(302);
  26.             $event->stopPropagation();
  27.         }
  28.     }
  29. }