vendor/shopware/storefront/Page/Product/ProductPageLoader.php line 158

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Page\Product;
  3. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  4. use Shopware\Core\Content\Product\Aggregate\ProductMedia\ProductMediaCollection;
  5. use Shopware\Core\Content\Product\Aggregate\ProductMedia\ProductMediaEntity;
  6. use Shopware\Core\Content\Product\Exception\ProductNotFoundException;
  7. use Shopware\Core\Content\Product\SalesChannel\CrossSelling\AbstractProductCrossSellingRoute;
  8. use Shopware\Core\Content\Product\SalesChannel\Detail\AbstractProductDetailRoute;
  9. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  10. use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionCollection;
  11. use Shopware\Core\Content\Property\PropertyGroupCollection;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Shopware\Core\Framework\Feature;
  15. use Shopware\Core\Framework\Log\Package;
  16. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  17. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  18. use Shopware\Storefront\Page\GenericPageLoaderInterface;
  19. use Shopware\Storefront\Page\Product\Review\ProductReviewLoader;
  20. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  21. use Symfony\Component\HttpFoundation\Request;
  22. #[Package('storefront')]
  23. class ProductPageLoader
  24. {
  25.     /**
  26.      * @internal
  27.      */
  28.     public function __construct(private readonly GenericPageLoaderInterface $genericLoader, private readonly EventDispatcherInterface $eventDispatcher, private readonly AbstractProductDetailRoute $productDetailRoute, private readonly ProductReviewLoader $productReviewLoader, private readonly AbstractProductCrossSellingRoute $crossSellingRoute)
  29.     {
  30.     }
  31.     /**
  32.      * @throws CategoryNotFoundException
  33.      * @throws InconsistentCriteriaIdsException
  34.      * @throws MissingRequestParameterException
  35.      * @throws ProductNotFoundException
  36.      */
  37.     public function load(Request $requestSalesChannelContext $context): ProductPage
  38.     {
  39.         $productId $request->attributes->get('productId');
  40.         if (!$productId) {
  41.             throw new MissingRequestParameterException('productId''/productId');
  42.         }
  43.         $criteria = (new Criteria())
  44.             ->addAssociation('manufacturer.media')
  45.             ->addAssociation('options.group')
  46.             ->addAssociation('properties.group')
  47.             ->addAssociation('mainCategories.category')
  48.             ->addAssociation('media');
  49.         $this->eventDispatcher->dispatch(new ProductPageCriteriaEvent($productId$criteria$context));
  50.         $result $this->productDetailRoute->load($productId$request$context$criteria);
  51.         $product $result->getProduct();
  52.         if ($product->getMedia()) {
  53.             $product->getMedia()->sort(fn (ProductMediaEntity $aProductMediaEntity $b) => $a->getPosition() <=> $b->getPosition());
  54.         }
  55.         if ($product->getMedia() && $product->getCover()) {
  56.             $product->setMedia(new ProductMediaCollection(array_merge(
  57.                 [$product->getCover()->getId() => $product->getCover()],
  58.                 $product->getMedia()->getElements()
  59.             )));
  60.         }
  61.         if ($category $product->getSeoCategory()) {
  62.             $request->request->set('navigationId'$category->getId());
  63.         }
  64.         $page $this->genericLoader->load($request$context);
  65.         $page ProductPage::createFrom($page);
  66.         $page->setProduct($product);
  67.         $page->setConfiguratorSettings($result->getConfigurator() ?? new PropertyGroupCollection());
  68.         $page->setNavigationId($product->getId());
  69.         if (!Feature::isActive('v6.6.0.0')) {
  70.             $this->loadDefaultAdditions($product$page$request$context);
  71.         } elseif ($cmsPage $product->getCmsPage()) {
  72.             $page->setCmsPage($cmsPage);
  73.         }
  74.         $this->loadOptions($page);
  75.         $this->loadMetaData($page);
  76.         $this->eventDispatcher->dispatch(
  77.             new ProductPageLoadedEvent($page$context$request)
  78.         );
  79.         return $page;
  80.     }
  81.     private function loadOptions(ProductPage $page): void
  82.     {
  83.         $options = new PropertyGroupOptionCollection();
  84.         if (($optionIds $page->getProduct()->getOptionIds()) === null) {
  85.             $page->setSelectedOptions($options);
  86.             return;
  87.         }
  88.         foreach ($page->getConfiguratorSettings() as $group) {
  89.             $groupOptions $group->getOptions();
  90.             if ($groupOptions === null) {
  91.                 continue;
  92.             }
  93.             foreach ($optionIds as $optionId) {
  94.                 $groupOption $groupOptions->get($optionId);
  95.                 if ($groupOption !== null) {
  96.                     $options->add($groupOption);
  97.                 }
  98.             }
  99.         }
  100.         $page->setSelectedOptions($options);
  101.     }
  102.     private function loadMetaData(ProductPage $page): void
  103.     {
  104.         $metaInformation $page->getMetaInformation();
  105.         if (!$metaInformation) {
  106.             return;
  107.         }
  108.         $metaDescription $page->getProduct()->getTranslation('metaDescription')
  109.             ?? $page->getProduct()->getTranslation('description');
  110.         $metaInformation->setMetaDescription((string) $metaDescription);
  111.         $metaInformation->setMetaKeywords((string) $page->getProduct()->getTranslation('keywords'));
  112.         if ((string) $page->getProduct()->getTranslation('metaTitle') !== '') {
  113.             $metaInformation->setMetaTitle((string) $page->getProduct()->getTranslation('metaTitle'));
  114.             return;
  115.         }
  116.         $metaTitleParts = [$page->getProduct()->getTranslation('name')];
  117.         foreach ($page->getSelectedOptions() as $option) {
  118.             $metaTitleParts[] = $option->getTranslation('name');
  119.         }
  120.         $metaTitleParts[] = $page->getProduct()->getProductNumber();
  121.         $metaInformation->setMetaTitle(implode(' | '$metaTitleParts));
  122.     }
  123.     /**
  124.      * @@deprecated tag:v6.6.0 - will be removed because cms page id will always be set
  125.      */
  126.     private function loadDefaultAdditions(SalesChannelProductEntity $productProductPage $pageRequest $requestSalesChannelContext $context): void
  127.     {
  128.         if ($cmsPage $product->getCmsPage()) {
  129.             $page->setCmsPage($cmsPage);
  130.             return;
  131.         }
  132.         $request->request->set('parentId'$product->getParentId());
  133.         $reviews $this->productReviewLoader->load($request$context);
  134.         $reviews->setParentId($product->getParentId() ?? $product->getId());
  135.         $page->setReviews($reviews);
  136.         $crossSellings $this->crossSellingRoute->load($product->getId(), new Request(), $context, new Criteria());
  137.         $page->setCrossSellings($crossSellings->getResult());
  138.     }
  139. }