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

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