src/Core/Content/Product/SalesChannel/Listing/ProductListingRoute.php line 38

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\Product\SalesChannel\Listing;
  3. use Shopware\Core\Content\Category\CategoryDefinition;
  4. use Shopware\Core\Content\Category\CategoryEntity;
  5. use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition;
  6. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  7. use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter;
  8. use Shopware\Core\Content\ProductStream\Service\ProductStreamBuilderInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\Log\Package;
  13. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  14. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  18. #[Route(defaults: ['_routeScope' => ['store-api']])]
  19. #[Package('inventory')]
  20. class ProductListingRoute extends AbstractProductListingRoute
  21. {
  22.     /**
  23.      * @internal
  24.      */
  25.     public function __construct(private readonly ProductListingLoader $listingLoader, private readonly EventDispatcherInterface $eventDispatcher, private readonly EntityRepository $categoryRepository, private readonly ProductStreamBuilderInterface $productStreamBuilder)
  26.     {
  27.     }
  28.     public function getDecorated(): AbstractProductListingRoute
  29.     {
  30.         throw new DecorationPatternException(self::class);
  31.     }
  32.     #[Route(path'/store-api/product-listing/{categoryId}'name'store-api.product.listing'methods: ['POST'], defaults: ['_entity' => 'product'])]
  33.     public function load(string $categoryIdRequest $requestSalesChannelContext $contextCriteria $criteria): ProductListingRouteResponse
  34.     {
  35.         $criteria->addFilter(
  36.             new ProductAvailableFilter($context->getSalesChannel()->getId(), ProductVisibilityDefinition::VISIBILITY_ALL)
  37.         );
  38.         $criteria->setTitle('product-listing-route::loading');
  39.         $categoryCriteria = new Criteria([$categoryId]);
  40.         $categoryCriteria->setTitle('product-listing-route::category-loading');
  41.         /** @var CategoryEntity $category */
  42.         $category $this->categoryRepository->search($categoryCriteria$context->getContext())->first();
  43.         $streamId $this->extendCriteria($context$criteria$category);
  44.         $entities $this->listingLoader->load($criteria$context);
  45.         $result ProductListingResult::createFrom($entities);
  46.         $result->addState(...$entities->getStates());
  47.         $result->addCurrentFilter('navigationId'$categoryId);
  48.         $this->eventDispatcher->dispatch(
  49.             new ProductListingResultEvent($request$result$context)
  50.         );
  51.         $result->setStreamId($streamId);
  52.         return new ProductListingRouteResponse($result);
  53.     }
  54.     private function extendCriteria(SalesChannelContext $salesChannelContextCriteria $criteriaCategoryEntity $category): ?string
  55.     {
  56.         if ($category->getProductAssignmentType() === CategoryDefinition::PRODUCT_ASSIGNMENT_TYPE_PRODUCT_STREAM && $category->getProductStreamId() !== null) {
  57.             $filters $this->productStreamBuilder->buildFilters(
  58.                 $category->getProductStreamId(),
  59.                 $salesChannelContext->getContext()
  60.             );
  61.             $criteria->addFilter(...$filters);
  62.             return $category->getProductStreamId();
  63.         }
  64.         $criteria->addFilter(
  65.             new EqualsFilter('product.categoriesRo.id'$category->getId())
  66.         );
  67.         return null;
  68.     }
  69. }