Skip to content
This repository was archived by the owner on Feb 8, 2023. It is now read-only.

Commit 5788cc9

Browse files
author
giaphn
committed
- Update HTML Sitemap
1 parent 28a22da commit 5788cc9

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

Block/Sitemap.php

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public function getCategoryCollection()
183183
->addFieldToFilter('entity_id', ['in' => $storeRootCategory->getAllChildren(true)])
184184
->addFieldToFilter('is_active', 1)
185185
->addFieldToFilter('include_in_menu', 1)
186-
->addFieldToFilter('entity_id', ['nin' => [$storeRootCategoryId]])->setOrder('path');
186+
->addFieldToFilter('entity_id', ['nin' => [$storeRootCategoryId]]);
187187

188188
$excludeCategories = $this->_helper->getHtmlSitemapConfig('category_page');
189189
if (!empty($excludeCategories)) {
@@ -192,29 +192,73 @@ public function getCategoryCollection()
192192
$excludeCategories
193193
));
194194

195+
$allExcludeIds = '';
195196
foreach ($excludeCategories as $excludeCategory) {
196197
try {
197198
$testRegex = preg_match($excludeCategory, '');
198199
if ($testRegex) {
199-
$excludeCategoriesIds = $this->filterCategoryWithRegex($excludeCategory);
200-
if (count($excludeCategoriesIds)) {
201-
$categoryCollection->addFieldToFilter('entity_id', ['nin' => $excludeCategoriesIds]);
202-
}
200+
$allExcludeIds .= '-' . $this->filterCategoryWithRegex($excludeCategory);
201+
} else {
202+
$excludePath = $this->getExcludePath($excludeCategory);
203+
$allExcludeIds .= '-' . $this->filterCategoryWithPath($excludePath, $categoryCollection);
203204
}
204205
} catch (Exception $e) {
205206
$excludePath = $this->getExcludePath($excludeCategory);
206-
$categoryCollection->addFieldToFilter('url_path', ['nlike' => '%' . $excludePath . '%']);
207+
$allExcludeIds .= '-' . $this->filterCategoryWithPath($excludePath, $categoryCollection);
207208
}
208209
}
210+
211+
$excludeIds = explode('-', $allExcludeIds);
212+
$categoryCollection->addFieldToFilter('entity_id', ['nin' => $excludeIds]);
213+
}
214+
215+
return $this->_categoryCollection->create()->addAttributeToSelect('*')
216+
->addFieldToFilter('entity_id', ['in' => $categoryCollection->getAllIds()])->setOrder('path');
217+
}
218+
219+
/**
220+
* @param $path
221+
* @param $categoryCollection
222+
*
223+
* @return string
224+
*/
225+
protected function filterCategoryWithPath($path, $categoryCollection)
226+
{
227+
$excludeIds = [];
228+
foreach ($categoryCollection as $category) {
229+
if ($this->isExcludeCategory($category, $path)) {
230+
$excludeIds[] = $category->getData('entity_id');
231+
}
209232
}
210233

211-
return $categoryCollection;
234+
return implode('-', $excludeIds);
235+
}
236+
237+
/**
238+
* @param $category
239+
* @param $path
240+
*
241+
* @return bool
242+
*/
243+
public function isExcludeCategory($category, $path)
244+
{
245+
$filterPath = explode('/', $path);
246+
$categoryPath = $category->getUrlPath();
247+
$categoryPath = explode('/', $categoryPath);
248+
249+
foreach ($filterPath as $pathInfo) {
250+
if (!in_array($pathInfo, $categoryPath)) {
251+
return false;
252+
}
253+
}
254+
255+
return true;
212256
}
213257

214258
/**
215259
* @param $regex
216260
*
217-
* @return array
261+
* @return string
218262
* @throws LocalizedException
219263
* @throws NoSuchEntityException
220264
*/
@@ -229,7 +273,7 @@ protected function filterCategoryWithRegex($regex)
229273
}
230274
}
231275

232-
return $excludeCategoriesIds;
276+
return implode('-', $excludeCategoriesIds);
233277
}
234278

235279
/**

etc/adminhtml/system.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@
4242
<comment>
4343
<![CDATA[
4444
Display Category Pages in HTML Sitemap except those with the above selected string. Separated by a line break.
45-
<br/>E.g: Do not display with the category pages including /men/ such as:
46-
/men/top
47-
/men/top/t-shirt
48-
/clothes/men/
45+
<br/>E.g: Do not display with the category pages including /men/ such as: </br>
46+
/men/top</br>
47+
/men/top/t-shirt</br>
48+
/clothes/men/</br>
4949
Support Regular expression
5050
]]>
5151
</comment>

0 commit comments

Comments
 (0)