prepareCriteria example

$this->twigVariableParser = $parserFactory->getParser($twig);
    }

    /** * @param array<string|array<string, string>> $ids * * @return iterable<SeoUrlEntity> */
    public function generate(array $ids, string $template, SeoUrlRouteInterface $route, Context $context, SalesChannelEntity $salesChannel): iterable
    {
        $criteria = new Criteria($ids);
        $route->prepareCriteria($criteria$salesChannel);

        $config = $route->getConfig();

        $repository = $this->definitionRegistry->getRepository($config->getDefinition()->getEntityName());

        $associations = $this->getAssociations($template$repository->getDefinition());
        $criteria->addAssociations($associations);

        $criteria->setLimit(50);

        /** @var RepositoryIterator<LandingPageCollection|CategoryCollection|ProductCollection> $iterator */
        

    public function prepare(PrepareDataCollection $collection, Element $element, ShopContextInterface $context)
    {
        $type = $element->getConfig()->get(self::SLIDER_TYPE_KEY, self::TYPE_STATIC_PRODUCT);
        $key = ComponentHandlerInterface::CRITERIA_KEY . $element->getId();

        switch ($type) {
            case self::TYPE_PRODUCT_STREAM:
                $criteria = $this->generateCriteria($element$context);

                $productStreamId = $element->getConfig()->get('article_slider_stream');
                $this->productStreamRepository->prepareCriteria($criteria$productStreamId);

                // request multiple products by criteria                 $collection->getBatchRequest()->setCriteria($key$criteria);
                break;

            case self::TYPE_TOPSELLER:
            case self::TYPE_NEWCOMER:
            case self::TYPE_RANDOM:
            case self::TYPE_LOWEST_PRICE:
            case self::TYPE_HIGHEST_PRICE:
                $criteria = $this->generateCriteria($element$context);

                


        if (!$streamId) {
            return;
        }

        $context = $this->container->get(ContextServiceInterface::class)->getShopContext();

        $criteria = $this->container->get(CriteriaFactoryInterface::class)
            ->createCriteria($this->Request()$context);

        $this->container->get(Repository::class)->prepareCriteria($criteria$streamId);

        $products = $this->container->get(ProductSearchInterface::class)->search($criteria$context);

        $convertedProducts = $this->container->get(LegacyStructConverter::class)
            ->convertListProductStructList($products->getProducts());

        $this->View()->assign(['sArticles' => $convertedProducts, 'articles' => $convertedProducts]);
    }

    /** * Tag cloud by category * * @return void */
static::assertSame(ProductPageSeoUrlRoute::DEFAULT_TEMPLATE, $config->getTemplate());
        static::assertTrue($config->getSkipInvalid());
    }

    public function testCriteria(): void
    {
        $route = new ProductPageSeoUrlRoute($this->createMock(ProductDefinition::class));

        $criteria = new Criteria();
        $salesChannel = new SalesChannelEntity();
        $salesChannel->setId('test');
        $route->prepareCriteria($criteria$salesChannel);
        static::assertTrue($criteria->hasEqualsFilter('active'));

        static::assertTrue($criteria->hasEqualsFilter('visibilities.salesChannelId'));
    }

    public function testMappingWithInvalidEntity(): void
    {
        $route = new ProductPageSeoUrlRoute($this->createMock(ProductDefinition::class));

        static::expectException(\InvalidArgumentException::class);
        $route->getMapping(new ArrayEntity()new SalesChannelEntity());
    }
if (!$default) {
            return;
        }

        $this->Request()->setParam('sSort', $default->getId());
    }

    private function createCategoryStreamCriteria(int $categoryId, int $streamId, ShopContextInterface $context): Criteria
    {
        $criteria = $this->criteriaFactory->createCriteria($this->Request()$context);

        $this->productStreamRepository->prepareCriteria($criteria$streamId);

        $facets = $this->customFacetService->getFacetsOfCategories([$categoryId]$context);

        $facets = array_shift($facets);

        if (!\is_array($facets)) {
            $facets = [];
        }

        foreach ($facets as $facet) {
            $customFacet = $facet->getFacet();

            

    public function search(string $entityName, array $criteria): EntitySearchResult
    {
        $criteriaObject = $this->prepareCriteria($entityName$criteria);

        return $this->registry->getRepository($entityName)->search($criteriaObject$this->context);
    }

    /** * The `ids()` method allows you to search for the Ids of Entities that match a given criteria. * * @param string $entityName The name of the Entity you want to search for, e.g. `product` or `media`. * @param array<string, mixed> $criteria The criteria used for your search. * * @return IdSearchResult A `IdSearchResult` including all entity-ids that matched your criteria. * * @example repository-search-ids/script.twig Get the Ids of products with the given ProductNumber. */
static::assertSame(ProductPageSeoUrlRoute::DEFAULT_TEMPLATE, $config->getTemplate());
        static::assertTrue($config->getSkipInvalid());
    }

    public function testCriteria(): void
    {
        $route = new ProductPageSeoUrlRoute($this->createMock(ProductDefinition::class));

        $criteria = new Criteria();
        $salesChannel = new SalesChannelEntity();
        $salesChannel->setId('test');
        $route->prepareCriteria($criteria$salesChannel);
        static::assertTrue($criteria->hasEqualsFilter('active'));

        static::assertTrue($criteria->hasEqualsFilter('visibilities.salesChannelId'));
    }

    public function testMappingWithInvalidEntity(): void
    {
        $route = new ProductPageSeoUrlRoute($this->createMock(ProductDefinition::class));

        static::expectException(\InvalidArgumentException::class);
        $route->getMapping(new ArrayEntity()new SalesChannelEntity());
    }
$request,
                $context,
                $categoryId
            );
        }

        $factory = Shopware()->Container()->get(CriteriaFactoryInterface::class);
        $criteria = $factory->createCriteria($request$context);
        $criteria->limit(null);

        $streamRepository = Shopware()->Container()->get(Repository::class);
        $streamRepository->prepareCriteria($criteria$streamId);

        return $criteria;
    }

    private function getStreamIdOfCategory(int $categoryId): ?int
    {
        $streamId = $this->db->fetchOne('SELECT `stream_id` FROM `s_categories` WHERE id = ?', [$categoryId]);

        if (empty($streamId)) {
            return null;
        }

        

    public function search(string $entityName, array $criteria): EntitySearchResult
    {
        $criteriaObject = $this->prepareCriteria($entityName$criteria);

        return $this->registry->getSalesChannelRepository($entityName)->search($criteriaObject$this->context);
    }

    /** * The `ids()` method allows you to search for the Ids of Entities that match a given criteria. * * @param string $entityName The name of the Entity you want to search for, e.g. `product` or `media`. * @param array<string, mixed> $criteria The criteria used for your search. * * @return IdSearchResult A `IdSearchResult` including all entity-ids that matched your criteria. * * @example store-search-ids/script.twig Get the Ids of products with the given ProductNumber. */
private function getProductStream($productStreamId$offset = 0, $limit = 100)
    {
        $context = Shopware()->Container()->get(ContextServiceInterface::class)->getShopContext();
        $factory = Shopware()->Container()->get(StoreFrontCriteriaFactoryInterface::class);

        $category = $context->getShop()->getCategory()->getId();
        $criteria = $factory->createBaseCriteria([$category]$context);
        $criteria->offset($offset)
                 ->limit($limit);

        $streamRepository = $this->get(Repository::class);
        $streamRepository->prepareCriteria($criteria$productStreamId);

        $result = Shopware()->Container()->get(ProductSearchInterface::class)->search($criteria$context);
        $data = Shopware()->Container()->get(LegacyStructConverter::class)->convertListProductStructList($result->getProducts());

        $count = $result->getTotalCount();

        if ($limit !== 0) {
            $pages = round($count / $limit);
        } else {
            $pages = 0;
        }

        

    public function testPrepareCriteria(): void
    {
        $navigationPageSeoUrlRoute = new NavigationPageSeoUrlRoute(
            new CategoryDefinition(),
            static::createStub(CategoryBreadcrumbBuilder::class)
        );

        $salesChannel = new SalesChannelEntity();

        $criteria = new Criteria();
        $navigationPageSeoUrlRoute->prepareCriteria($criteria$salesChannel);

        $filters = $criteria->getFilters();
        static::assertCount(2, $filters);

        $notFilter = $filters[0];
        static::assertInstanceOf(NotFilter::class$notFilter);

        static::assertEquals(MultiFilter::CONNECTION_OR, $notFilter->getOperator());

        $notFilterQueries = $notFilter->getQueries();
        static::assertCount(2, $notFilterQueries);

        
Home | Imprint | This part of the site doesn't use cookies.