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

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