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

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\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.  * @RouteScope(scopes={"store-api"})
  30.  */
  31. class ProductDetailRoute extends AbstractProductDetailRoute
  32. {
  33.     /**
  34.      * @var SalesChannelRepositoryInterface
  35.      */
  36.     private $productRepository;
  37.     /**
  38.      * @var SystemConfigService
  39.      */
  40.     private $config;
  41.     /**
  42.      * @var ProductConfiguratorLoader
  43.      */
  44.     private $configuratorLoader;
  45.     /**
  46.      * @var CategoryBreadcrumbBuilder
  47.      */
  48.     private $breadcrumbBuilder;
  49.     /**
  50.      * @var SalesChannelCmsPageLoaderInterface
  51.      */
  52.     private $cmsPageLoader;
  53.     /**
  54.      * @var ProductDefinition
  55.      */
  56.     private $productDefinition;
  57.     public function __construct(
  58.         SalesChannelRepositoryInterface $productRepository,
  59.         SystemConfigService $config,
  60.         ProductConfiguratorLoader $configuratorLoader,
  61.         CategoryBreadcrumbBuilder $breadcrumbBuilder,
  62.         SalesChannelCmsPageLoaderInterface $cmsPageLoader,
  63.         SalesChannelProductDefinition $productDefinition
  64.     ) {
  65.         $this->productRepository $productRepository;
  66.         $this->config $config;
  67.         $this->configuratorLoader $configuratorLoader;
  68.         $this->breadcrumbBuilder $breadcrumbBuilder;
  69.         $this->cmsPageLoader $cmsPageLoader;
  70.         $this->productDefinition $productDefinition;
  71.     }
  72.     public function getDecorated(): AbstractProductDetailRoute
  73.     {
  74.         throw new DecorationPatternException(self::class);
  75.     }
  76.     /**
  77.      * @Since("6.3.2.0")
  78.      * @Entity("product")
  79.      * @OA\Post(
  80.      *      path="/product/{productId}",
  81.      *      summary="Fetch a single product",
  82.      *      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.",
  83.      *      operationId="readProductDetail",
  84.      *      tags={"Store API","Product"},
  85.      *      @OA\Parameter(
  86.      *          name="productId",
  87.      *          description="Product ID",
  88.      *          @OA\Schema(type="string"),
  89.      *          in="path",
  90.      *          required=true
  91.      *      ),
  92.      *      @OA\Response(
  93.      *          response="200",
  94.      *          description="Product information along with variant groups and options",
  95.      *          @OA\JsonContent(ref="#/components/schemas/ProductDetailResponse")
  96.      *     )
  97.      * )
  98.      * @Route("/store-api/product/{productId}", name="store-api.product.detail", methods={"POST"})
  99.      */
  100.     public function load(string $productIdRequest $requestSalesChannelContext $contextCriteria $criteria): ProductDetailRouteResponse
  101.     {
  102.         $productId $this->findBestVariant($productId$context);
  103.         $this->addFilters($context$criteria);
  104.         $criteria->setIds([$productId]);
  105.         $product $this->productRepository
  106.             ->search($criteria$context)
  107.             ->first();
  108.         if (!$product instanceof SalesChannelProductEntity) {
  109.             throw new ProductNotFoundException($productId);
  110.         }
  111.         $product->setSeoCategory(
  112.             $this->breadcrumbBuilder->getProductSeoCategory($product$context)
  113.         );
  114.         $configurator $this->configuratorLoader->load($product$context);
  115.         $pageId $product->getCmsPageId();
  116.         if ($pageId) {
  117.             // clone product to prevent recursion encoding (see NEXT-17603)
  118.             $resolverContext = new EntityResolverContext($context$request$this->productDefinition, clone $product);
  119.             $pages $this->cmsPageLoader->load(
  120.                 $request,
  121.                 $this->createCriteria($pageId$request),
  122.                 $context,
  123.                 $product->getTranslation('slotConfig'),
  124.                 $resolverContext
  125.             );
  126.             if ($page $pages->first()) {
  127.                 $product->setCmsPage($page);
  128.             }
  129.         }
  130.         return new ProductDetailRouteResponse($product$configurator);
  131.     }
  132.     private function addFilters(SalesChannelContext $contextCriteria $criteria): void
  133.     {
  134.         $criteria->addFilter(
  135.             new ProductAvailableFilter($context->getSalesChannel()->getId(), ProductVisibilityDefinition::VISIBILITY_LINK)
  136.         );
  137.         $salesChannelId $context->getSalesChannel()->getId();
  138.         $hideCloseoutProductsWhenOutOfStock $this->config->get('core.listing.hideCloseoutProductsWhenOutOfStock'$salesChannelId);
  139.         if ($hideCloseoutProductsWhenOutOfStock) {
  140.             $filter = new ProductCloseoutFilter();
  141.             $filter->addQuery(new EqualsFilter('product.parentId'null));
  142.             $criteria->addFilter($filter);
  143.         }
  144.     }
  145.     /**
  146.      * @throws InconsistentCriteriaIdsException
  147.      */
  148.     private function findBestVariant(string $productIdSalesChannelContext $context): string
  149.     {
  150.         $criteria = (new Criteria())
  151.             ->addFilter(new EqualsFilter('product.parentId'$productId))
  152.             ->addSorting(new FieldSorting('product.price'))
  153.             ->addSorting(new FieldSorting('product.available'))
  154.             ->setLimit(1);
  155.         $variantId $this->productRepository->searchIds($criteria$context);
  156.         return $variantId->firstId() ?? $productId;
  157.     }
  158.     private function createCriteria(string $pageIdRequest $request): Criteria
  159.     {
  160.         $criteria = new Criteria([$pageId]);
  161.         $criteria->setTitle('product::cms-page');
  162.         $slots $request->get('slots');
  163.         if (\is_string($slots)) {
  164.             $slots explode('|'$slots);
  165.         }
  166.         if (!empty($slots) && \is_array($slots)) {
  167.             $criteria
  168.                 ->getAssociation('sections.blocks')
  169.                 ->addFilter(new EqualsAnyFilter('slots.id'$slots));
  170.         }
  171.         return $criteria;
  172.     }
  173. }