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

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