@@ -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 /**
0 commit comments