custom/plugins/FourtwosixThemeCustomization/src/Subscriber/ProductDetailTemplateSubscriber.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace FourtwosixThemeCustomization\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Content\Product\ProductDefinition;
  6. use Shopware\Core\System\SystemConfig\SystemConfigService;
  7. use Swag\CmsExtensions\Service\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderBlockRuleDecorator as SalesChannelCmsPageLoader;
  8. use Shopware\Core\Content\Product\ProductEvents;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. class ProductDetailTemplateSubscriber implements EventSubscriberInterface
  11. {
  12.     /** @var SystemConfigService $systemConfigService */
  13.     private SystemConfigService $systemConfigService;
  14.     /** @var RequestStack $requestStack */
  15.     private RequestStack $requestStack;
  16.     public function __construct(SystemConfigService $systemConfigServiceRequestStack $requestStack)
  17.     {
  18.         $this->systemConfigService $systemConfigService;
  19.         $this->requestStack $requestStack;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             'sales_channel.' ProductEvents::PRODUCT_LOADED_EVENT => 'onProductDataLoaded',
  25.         ];
  26.     }
  27.     public function onProductDataLoaded($event) {
  28.         $request $this->requestStack->getCurrentRequest();
  29.         if(!empty($request) && $request->get("_route") == "frontend.detail.page") {
  30.             $products $event->getEntities();
  31.             if(count($products) == 1) {
  32.                 $salesChannelId $event->getSalesChannelContext()->getSalesChannelId();
  33.                 $newDefaultCmsPage $this->systemConfigService->get('FourtwosixThemeCustomization.config.standardProductDetail'$salesChannelId);
  34.                 if(!empty($newDefaultCmsPage)) {
  35.                     $products[0]->setCmsPageId($newDefaultCmsPage);
  36.                 }
  37.             }
  38.         }
  39.     }
  40. }