src/Core/Content/Product/SalesChannel/Detail/ProductDetailRoute.php line 47

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