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

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