addFacet example



    /** * {@inheritdoc} */
    public function handle(
        CriteriaPartInterface $criteriaPart,
        Criteria $criteria,
        Search $search,
        ShopContextInterface $context
    ) {
        $this->addFacet($criteriaPart$search);
    }

    /** * {@inheritdoc} */
    public function hydrate(
        array $elasticResult,
        ProductNumberSearchResult $result,
        Criteria $criteria,
        ShopContextInterface $context
    ) {
        
if (!\array_key_exists($criteriaFacet->getName()$elasticResult['aggregations'])) {
                continue;
            }

            $data = $elasticResult['aggregations'][$criteriaFacet->getName()];

            $facetResult = $this->createRangeFacet($criteriaFacet$data$criteria);
            if (!$facetResult) {
                continue;
            }

            $result->addFacet($facetResult);
        }
    }

    /** * @param WeightFacet|WidthFacet|LengthFacet|HeightFacet $facet * @param array<string, string> $stats */
    private function createRangeFacet(FacetInterface $facet, array $stats, Criteria $criteria): ?RangeFacetResult
    {
        $name = $facet->getName();

        


            if (!$criteria->hasFacet($key)) {
                continue;
            }

            $facet = $criteria->getFacet($key);
            if (!$facet instanceof CombinedConditionFacet) {
                continue;
            }

            $result->addFacet(
                new BooleanFacetResult(
                    $facet->getName(),
                    $facet->getRequestParameter(),
                    $criteria->hasCondition($facet->getName()),
                    $facet->getLabel()
                )
            );
        }
    }

    private function addQuery(
        
return;
        }

        $data = $elasticResult['aggregations']['has_available_variant_filter']['has_available_variant_count'];

        if ($data['value'] <= 0) {
            return;
        }

        $criteriaPart = $this->createFacet($criteria);

        $result->addFacet($criteriaPart);
    }

    private function createFacet(Criteria $criteria): BooleanFacetResult
    {
        $facet = $criteria->getFacet('immediate_delivery');
        if ($facet instanceof ImmediateDeliveryFacet && !empty($facet->getLabel())) {
            $label = $facet->getLabel();
        } else {
            $label = $this->snippetManager
                ->getNamespace('frontend/listing/facet_labels')
                ->get('immediate_delivery', 'Immediate delivery');
        }
/** @var CustomFacet[] $customFacets */
            $customFacets = array_shift($customFacets);
        } else {
            $customFacets = $this->facetService->getAllCategoryFacets($context);
        }

        foreach ($customFacets as $customFacet) {
            if (!$customFacet->getFacet()) {
                continue;
            }
            $facet = $customFacet->getFacet();
            $criteria->addFacet($facet);

            if ($facet instanceof ProductAttributeFacet) {
                $this->handleProductAttributeFacet($request$criteria$facet);
            } elseif ($facet instanceof CombinedConditionFacet) {
                $this->handleCombinedConditionFacet($request$criteria$facet);
            }
        }
    }

    /** * @return bool */
$facet = $this->categoryTreeFacetResultBuilder->buildFacetResult(
            $categories,
            $this->getFilteredIds($criteria),
            $context->getShop()->getCategory()->getId(),
            $categoryFacet
        );

        if (!$facet instanceof FacetResultInterface) {
            return;
        }
        $result->addFacet($facet);
    }

    /** * @param array<int> $ids * * @return array<int> */
    private function filterSystemCategories(array $ids, ShopContextInterface $context): array
    {
        $system = array_merge(
            [$context->getShop()->getCategory()->getId()],
            
$facets = array_shift($facets);

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

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

            if ($customFacet instanceof FacetInterface) {
                $criteria->addFacet($customFacet);
            }
        }

        $this->facetFilter->add($criteria);

        $criteria->removeFacet('category');

        return $criteria;
    }

    /** * @param array<string, mixed> $categoryContent */


        $data = $this->client->search(
            $arguments
        );

        $data = $data['hits']['hits'];

        $properties = $this->hydrateProperties($data$ids);
        $actives = $this->getFilteredValues($criteria);
        $criteriaPart = $this->createCollectionResult($properties$actives);
        $result->addFacet($criteriaPart);
    }

    /** * @param array[] $data * @param int[] $optionIds * * @return Group[] */
    private function hydrateProperties(array $data, array $optionIds): array
    {
        $groups = [];
        
$groups = $this->gateway->getOptions($ids$context);
        if (empty($groups)) {
            return;
        }

        $groups = array_filter($groupsfunction DGroup $group) use ($facet) {
            return \in_array($group->getId()$facet->getGroupIds(), true);
        });

        $actives = $this->getFilteredValues($criteria);
        $facet = $this->createCollectionResult($facet$groups$actives);
        $result->addFacet($facet);
    }

    /** * @param Group[] $groups * @param int[] $actives */
    private function createCollectionResult(
        VariantFacet $facet,
        array $groups,
        array $actives
    ): FacetResultGroup {
        
if (empty($buckets)) {
            return;
        }

        $ids = array_column($buckets, 'key');
        $manufacturers = $this->manufacturerService->getList($ids$context);

        $items = $this->createListItems($criteria$manufacturers);

        $criteriaPart = $this->createFacet($criteria$items);
        $result->addFacet($criteriaPart);
    }

    /** * @param Manufacturer[] $manufacturers * * @return array<ValueListItem> */
    private function createListItems(Criteria $criteria, array $manufacturers): array
    {
        $actives = [];
        $condition = $criteria->getCondition('manufacturer');
        
return;
        }

        $data = $elasticResult['aggregations']['shipping_free_filter']['shipping_free_count'];

        if ($data['value'] <= 0) {
            return;
        }

        $criteriaPart = $this->createFacet($criteria);

        $result->addFacet($criteriaPart);
    }

    private function createFacet(Criteria $criteria): BooleanFacetResult
    {
        $fieldName = $this->queryAliasMapper->getShortAlias('shippingFree') ?? 'shippingFree';

        $facet = $criteria->getFacet('shipping_free');
        if ($facet instanceof ShippingFreeFacet && !empty($facet->getLabel())) {
            $label = $facet->getLabel();
        } else {
            $label = $this->snippetManager
                
return;
        }
        if ($data['min'] == $data['max']) {
            return;
        }

        $criteriaPart = $this->createFacet(
            $criteria,
            round((float) $data['min'], 2),
            round((float) $data['max'], 2)
        );
        $result->addFacet($criteriaPart);
    }

    private function createFacet(Criteria $criteria, float $min, float $max): RangeFacetResult
    {
        $activeMin = $min;
        $activeMax = $max;

        $condition = $criteria->getCondition('price');
        if ($condition instanceof PriceCondition) {
            $activeMin = $condition->getMinPrice();
            $activeMax = $condition->getMaxPrice();
        }
$criteria = $this->container->get(CriteriaFactoryInterface::class)->createCriteria($this->Request()$context);

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

        $facets = $this->container->get(CustomFacetServiceInterface::class)->getFacetsOfCategories([$categoryId]$context);
        $facets = $facets[$categoryId];
        foreach ($facets as $facet) {
            $customFacet = $facet->getFacet();
            if ($customFacet instanceof FacetInterface) {
                $criteria->addFacet($customFacet);
            }
        }

        $facetFilter = $this->container->get('shopware_product_stream.facet_filter');
        $facetFilter->add($criteria);

        $criteria->setGeneratePartialFacets(
            $this->container->get(Shopware_Components_Config::class)->get('listingMode') === 'filter_ajax_reload'
        );

        if (!$this->Request()->get('loadFacets')) {
            

        if (!isset($elasticResult['aggregations']['vote_average'])) {
            return;
        }

        $data = $elasticResult['aggregations']['vote_average'];
        if (empty($data['buckets'])) {
            return;
        }

        $criteriaPart = $this->createFacet($criteria$data['buckets']);
        $result->addFacet($criteriaPart);
    }

    /** * @param array<string, mixed> $buckets */
    private function createFacet(Criteria $criteria, array $buckets): RadioFacetResult
    {
        $activeAverage = null;
        $condition = $criteria->getCondition('vote_average');
        if ($condition instanceof VoteAverageCondition) {
            $activeAverage = $condition->getAverage();
        }
Home | Imprint | This part of the site doesn't use cookies.