Skip to content

Commit 8136e26

Browse files
committed
MAGE-640 Add auto anchoring logic to support categoryPageId filter with InstantSearch
1 parent 13e8c2d commit 8136e26

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

Helper/Entity/ProductHelper.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,24 @@ function ($path) use ($storeId) { return implode($this->configHelper->getCategor
834834
);
835835
}
836836

837+
/**
838+
* Take an array of paths where each element is an array of parent-child hierarchies and
839+
* append to the top level array each possible parent iteration.
840+
* This serves to emulate anchoring in Magento in order to use category page id filtering
841+
* without explicit category assignment.
842+
*
843+
* @param array $paths
844+
* @return array
845+
*/
846+
protected function autoAnchorParentCategories(array $paths): array {
847+
foreach ($paths as $path) {
848+
for ($i = count($path) - 1; $i > 0; $i--) {
849+
$paths[] = array_slice($path,0, $i);
850+
}
851+
}
852+
return $this->dedupePaths($paths);
853+
}
854+
837855
/**
838856
* @param array $algoliaData Data for product object to be serialized to Algolia index
839857
* @param Product $product
@@ -852,7 +870,8 @@ protected function addCategoryData(array $algoliaData, Product $product): array
852870
$algoliaData['categoryIds'] = array_values(array_unique($categoryData['categoryIds']));
853871

854872
if ($this->configHelper->isVisualMerchEnabled($storeId)) {
855-
$algoliaData[$this->configHelper->getCategoryPageIdAttributeName($storeId)] = $this->flattenCategoryPaths($categoryData['categoriesWithPath'], $storeId);
873+
$autoAnchorPaths = $this->autoAnchorParentCategories($categoryData['categoriesWithPath']);
874+
$algoliaData[$this->configHelper->getCategoryPageIdAttributeName($storeId)] = $this->flattenCategoryPaths($autoAnchorPaths, $storeId);
856875
}
857876

858877
return $algoliaData;

0 commit comments

Comments
 (0)