/** * @param ListProduct[] $listProducts * @param ShopContextInterface $context * @return Category[] indexed by product id */ public function getList($listProducts, ShopContextInterface $context) { $ids = array_map(function (ListProduct $product) { return $product->getId(); }, $listProducts); //select all seo category ids, indexed by product id $ids = $this->getCategoryIds($ids, $context); //now select all category data for the selected ids $categories = $this->categoryService->getList($ids, $context); $result = []; foreach ($ids as $productId => $categoryId) { if (!isset($categories[$categoryId])) { continue; } $result[$productId] = $categories[$categoryId]; } return $result; }
/** * Generates the facet for the \Shopware\Bundle\SearchBundle\Facet;\Category class. * Displays how many products are assigned to the children categories. * * The handler use the category ids of the \Shopware\Bundle\SearchBundle\Condition\Category. * If no \Shopware\Bundle\SearchBundle\Condition\Category is set, the handler uses as default the id 1. * * @param FacetInterface|Facet\CategoryFacet $facet * @param Criteria $criteria * @param ShopContextInterface $context * @return TreeFacetResult */ public function generateFacet(FacetInterface $facet, Criteria $criteria, ShopContextInterface $context) { $ids = $this->getCategoryIds($criteria, $context); $categories = $this->categoryService->getList($ids, $context); $active = []; if ($criteria->hasCondition('category')) { /**@var $condition CategoryCondition*/ $condition = $criteria->getCondition('category'); $active = $condition->getCategoryIds(); } return $this->createTreeFacet($categories, $facet, $active); }
/** * Returns the category tree from the root until the category * with the provided id. Also loads siblings for elements in the * category path. * * @param int $id Id of the category to load * @return array Tree of categories */ public function sGetCategories($id) { $pathIds = $this->getCategoryPath($id); $grouped = $this->getCategoryIdsWithParent($pathIds); $ids = array_merge($pathIds, array_keys($grouped)); $context = $this->contextService->getShopContext(); $categories = $this->categoryService->getList($ids, $context); unset($grouped[$this->baseId]); $tree = $this->buildTree($grouped, $this->baseId); $result = $this->assignCategoriesToTree($categories, $tree, $pathIds, $this->getChildrenCountOfCategories($ids)); return $result; }
/** * {@inheritdoc} */ public function hydrate(array $elasticResult, ProductNumberSearchResult $result, Criteria $criteria, ShopContextInterface $context) { if (!isset($elasticResult['aggregations'])) { return; } if (!isset($elasticResult['aggregations']['agg_category'])) { return; } $data = $elasticResult['aggregations']['agg_category']['buckets']; $ids = $this->getCategoryIds($criteria, $data); $categories = $this->categoryService->getList($ids, $context); $active = []; if ($criteria->hasCondition('category')) { /**@var $condition CategoryCondition*/ $condition = $criteria->getCondition('category'); $active = $condition->getCategoryIds(); } $criteriaPart = $this->createTreeFacet($categories, $active); $result->addFacet($criteriaPart); }