From eb015a2d3cfdc4c497b0f1923bb328d5bf60f38a Mon Sep 17 00:00:00 2001 From: DanieliMi <31039652+DanieliMi@users.noreply.github.com> Date: Fri, 11 Dec 2020 09:04:41 +0100 Subject: [PATCH] Abort if category is not a child of the root category --- src/Observers/UrlRewriteObserver.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Observers/UrlRewriteObserver.php b/src/Observers/UrlRewriteObserver.php index 11c311e..a2b9860 100644 --- a/src/Observers/UrlRewriteObserver.php +++ b/src/Observers/UrlRewriteObserver.php @@ -405,6 +405,11 @@ protected function resolveCategoryIds($categoryId, $topLevel = false, $storeView // load the data of the category with the passed ID $category = $this->getCategory($categoryId, $storeViewCode); + // if the category is not a child of the root category we have to skip it + if (!$this->isChildOfRootCategory($category)) { + return; + } + // create the product category relation for the current category $this->createProductCategoryRelation($category, $topLevel); @@ -417,6 +422,24 @@ protected function resolveCategoryIds($categoryId, $topLevel = false, $storeView } } + /** + * Check if category is a child of the root category + * + * @param array $category The category to check + * + * @return bool + */ + protected function isChildOfRootCategory($category) + { + // split the path into its segments + $categoryPathParts = explode('/', $category[MemberNames::PATH]); + + // load the root category + $rootCategory = $this->getRootCategory(); + + return $categoryPathParts[1] === $rootCategory[MemberNames::ENTITY_ID]; + } + /** * Adds the entity product relation if necessary. *