vendor/shopware/core/Content/Product/SalesChannel/Detail/ProductDetailRoute.php line 123

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\SalesChannel\Detail;
  3. use Shopware\Core\Content\Category\Service\CategoryBreadcrumbBuilder;
  4. use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext;
  5. use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
  6. use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
  7. use Shopware\Core\Content\Product\Exception\ProductNotFoundException;
  8. use Shopware\Core\Content\Product\ProductDefinition;
  9. use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter;
  10. use Shopware\Core\Content\Product\SalesChannel\ProductCloseoutFilter;
  11. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductDefinition;
  12. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  18. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  19. use Shopware\Core\Framework\Routing\Annotation\Entity;
  20. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  21. use Shopware\Core\Framework\Routing\Annotation\Since;
  22. use Shopware\Core\Profiling\Profiler;
  23. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  24. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  25. use Shopware\Core\System\SystemConfig\SystemConfigService;
  26. use Symfony\Component\HttpFoundation\Request;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. /**
  29.  * @Route(defaults={"_routeScope"={"store-api"}})
  30.  */
  31. class ProductDetailRoute extends AbstractProductDetailRoute
  32. {
  33.     private SalesChannelRepositoryInterface $productRepository;
  34.     private SystemConfigService $config;
  35.     private ProductConfiguratorLoader $configuratorLoader;
  36.     private CategoryBreadcrumbBuilder $breadcrumbBuilder;
  37.     private SalesChannelCmsPageLoaderInterface $cmsPageLoader;
  38.     private ProductDefinition $productDefinition;
  39.     /**
  40.      * @internal
  41.      */
  42.     public function __construct(
  43.         SalesChannelRepositoryInterface $productRepository,
  44.         SystemConfigService $config,
  45.         ProductConfiguratorLoader $configuratorLoader,
  46.         CategoryBreadcrumbBuilder $breadcrumbBuilder,
  47.         SalesChannelCmsPageLoaderInterface $cmsPageLoader,
  48.         SalesChannelProductDefinition $productDefinition
  49.     ) {
  50.         $this->productRepository $productRepository;
  51.         $this->config $config;
  52.         $this->configuratorLoader $configuratorLoader;
  53.         $this->breadcrumbBuilder $breadcrumbBuilder;
  54.         $this->cmsPageLoader $cmsPageLoader;
  55.         $this->productDefinition $productDefinition;
  56.     }
  57.     public function getDecorated(): AbstractProductDetailRoute
  58.     {
  59.         throw new DecorationPatternException(self::class);
  60.     }
  61.     /**
  62.      * @Since("6.3.2.0")
  63.      * @Entity("product")
  64.      * @Route("/store-api/product/{productId}", name="store-api.product.detail", methods={"POST"})
  65.      */
  66.     public function load(string $productIdRequest $requestSalesChannelContext $contextCriteria $criteria): ProductDetailRouteResponse
  67.     {
  68.         return Profiler::trace('product-detail-route', function () use ($productId$request$context$criteria) {
  69.             $mainVariantId $this->checkVariantListingConfig($productId$context);
  70.             $productId $mainVariantId ?? $this->findBestVariant($productId$context);
  71.             $this->addFilters($context$criteria);
  72.             $criteria->setIds([$productId]);
  73.             $criteria->setTitle('product-detail-route');
  74.             $product $this->productRepository
  75.                 ->search($criteria$context)
  76.                 ->first();
  77.             if (!($product instanceof SalesChannelProductEntity)) {
  78.                 throw new ProductNotFoundException($productId);
  79.             }
  80.             $product->setSeoCategory(
  81.                 $this->breadcrumbBuilder->getProductSeoCategory($product$context)
  82.             );
  83.             $configurator $this->configuratorLoader->load($product$context);
  84.             $pageId $product->getCmsPageId();
  85.             if ($pageId) {
  86.                 // clone product to prevent recursion encoding (see NEXT-17603)
  87.                 $resolverContext = new EntityResolverContext($context$request$this->productDefinition, clone $product);
  88.                 $pages $this->cmsPageLoader->load(
  89.                     $request,
  90.                     $this->createCriteria($pageId$request),
  91.                     $context,
  92.                     $product->getTranslation('slotConfig'),
  93.                     $resolverContext
  94.                 );
  95.                 if ($page $pages->first()) {
  96.                     $product->setCmsPage($page);
  97.                 }
  98.             }
  99.             return new ProductDetailRouteResponse($product$configurator);
  100.         });
  101.     }
  102.     private function addFilters(SalesChannelContext $contextCriteria $criteria): void
  103.     {
  104.         $criteria->addFilter(
  105.             new ProductAvailableFilter($context->getSalesChannel()->getId(), ProductVisibilityDefinition::VISIBILITY_LINK)
  106.         );
  107.         $salesChannelId $context->getSalesChannel()->getId();
  108.         $hideCloseoutProductsWhenOutOfStock $this->config->get('core.listing.hideCloseoutProductsWhenOutOfStock'$salesChannelId);
  109.         if ($hideCloseoutProductsWhenOutOfStock) {
  110.             $filter = new ProductCloseoutFilter();
  111.             $filter->addQuery(new EqualsFilter('product.parentId'null));
  112.             $criteria->addFilter($filter);
  113.         }
  114.     }
  115.     /**
  116.      * @throws InconsistentCriteriaIdsException
  117.      */
  118.     private function checkVariantListingConfig(string $productIdSalesChannelContext $context): ?string
  119.     {
  120.         /** @var SalesChannelProductEntity|null $product */
  121.         $product $this->productRepository->search(new Criteria([$productId]), $context)->first();
  122.         if ($product === null || $product->getParentId() !== null) {
  123.             return null;
  124.         }
  125.         if (($listingConfig $product->getVariantListingConfig()) === null || $listingConfig->getDisplayParent() !== true) {
  126.             return null;
  127.         }
  128.         return $listingConfig->getMainVariantId();
  129.     }
  130.     /**
  131.      * @throws InconsistentCriteriaIdsException
  132.      */
  133.     private function findBestVariant(string $productIdSalesChannelContext $context): string
  134.     {
  135.         $criteria = (new Criteria())
  136.             ->addFilter(new EqualsFilter('product.parentId'$productId))
  137.             ->addSorting(new FieldSorting('product.price'))
  138.             ->addSorting(new FieldSorting('product.available'))
  139.             ->setLimit(1);
  140.         $criteria->setTitle('product-detail-route::find-best-variant');
  141.         $variantId $this->productRepository->searchIds($criteria$context);
  142.         return $variantId->firstId() ?? $productId;
  143.     }
  144.     private function createCriteria(string $pageIdRequest $request): Criteria
  145.     {
  146.         $criteria = new Criteria([$pageId]);
  147.         $criteria->setTitle('product::cms-page');
  148.         $slots $request->get('slots');
  149.         if (\is_string($slots)) {
  150.             $slots explode('|'$slots);
  151.         }
  152.         if (!empty($slots) && \is_array($slots)) {
  153.             $criteria
  154.                 ->getAssociation('sections.blocks')
  155.                 ->addFilter(new EqualsAnyFilter('slots.id'$slots));
  156.         }
  157.         return $criteria;
  158.     }
  159. }