Skip to content

Commit ffe173b

Browse files
committed
MAGE-569 Implement recursive backend retrieval of URLs for all subcats
1 parent e3717cf commit ffe173b

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

Block/Configuration.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,40 @@ public function isSearchPage()
3030
return false;
3131
}
3232

33+
/**
34+
* @param \Magento\Catalog\Model\Category $cat
35+
* @return string
36+
*/
37+
protected function initCategoryParentPath(\Magento\Catalog\Model\Category $cat): string {
38+
$path = '';
39+
foreach ($cat->getPathIds() as $treeCategoryId) {
40+
if ($path) {
41+
$path .= $this->getConfigHelper()->getCategorySeparator($this->getStoreId());
42+
}
43+
$path .= $this->getCategoryHelper()->getCategoryName($treeCategoryId, $this->getStoreId());
44+
}
45+
return $path;
46+
}
47+
48+
/**
49+
* @param \Magento\Catalog\Model\Category $cat
50+
* @param string $parent
51+
* @param array $arr
52+
* @return array
53+
*/
54+
protected function getChildCategoryUrls(\Magento\Catalog\Model\Category $cat, string $parent = '', array $arr = array()): array {
55+
if (!$parent) {
56+
$parent = $this->initCategoryParentPath($cat);
57+
}
58+
59+
foreach ($cat->getChildrenCategories() as $child) {
60+
$key = $parent ? $parent . $this->getConfigHelper()->getCategorySeparator() . $child->getName() : $child ->getName();
61+
$arr[$key]['url'] = $child->getUrl();
62+
$arr = array_merge($arr, $this->getChildCategoryUrls($child, $key, $arr));
63+
}
64+
return $arr;
65+
}
66+
3367
public function getConfiguration()
3468
{
3569
$config = $this->getConfigHelper();
@@ -70,6 +104,7 @@ public function getConfiguration()
70104
$level = '';
71105
$categoryId = '';
72106
$parentCategoryName = '';
107+
$childCategories = [];
73108

74109
$addToCartParams = $this->getAddToCartParams();
75110

@@ -88,6 +123,7 @@ public function getConfiguration()
88123

89124
if ($category && $category->getDisplayMode() !== 'PAGE') {
90125
$category->getUrlInstance()->setStore($this->getStoreId());
126+
$childCategories = $this->getChildCategoryUrls($category);
91127

92128
$categoryId = $category->getId();
93129

@@ -236,6 +272,7 @@ public function getConfiguration()
236272
'path' => $path,
237273
'level' => $level,
238274
'parentCategory' => $parentCategoryName,
275+
'childCategories' => $childCategories
239276
],
240277
'showCatsNotIncludedInNavigation' => $config->showCatsNotIncludedInNavigation(),
241278
'showSuggestionsOnNoResultsPage' => $config->showSuggestionsOnNoResultsPage(),

view/frontend/web/instantsearch.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,13 +463,21 @@ requirejs(['algoliaBundle', 'Magento_Catalog/js/price-utils'], function (algolia
463463
sortBy: ['name:asc'],
464464
transformItems(items) {
465465
return (algoliaConfig.isCategoryPage)
466-
? findRoot(items)
466+
? findRoot(items).map(
467+
item => {
468+
if (true) {
469+
item.categoryUrl = algoliaConfig.request.childCategories[item.value]['url'];
470+
}
471+
console.log('item:', item);
472+
return item;
473+
}
474+
)
467475
: items;
468476
},
469477
};
470478

471479
hierarchicalMenuParams.templates.item = '' +
472-
'<a class="{{cssClasses.link}} {{#isRefined}}{{cssClasses.link}}--selected{{/isRefined}}" href="{{url}}">{{label}}' + ' ' +
480+
'<a class="{{cssClasses.link}} {{#isRefined}}{{cssClasses.link}}--selected{{/isRefined}}" href="{{categoryUrl}}">{{label}}' + ' ' +
473481
'<span class="{{cssClasses.count}}">{{#helpers.formatNumber}}{{count}}{{/helpers.formatNumber}}</span>' +
474482
'</a>';
475483
hierarchicalMenuParams.panelOptions = {

0 commit comments

Comments
 (0)