diff --git a/app/code/Magento/Catalog/Model/Category.php b/app/code/Magento/Catalog/Model/Category.php index c4d6742a02dd3..ff840635e4c70 100644 --- a/app/code/Magento/Catalog/Model/Category.php +++ b/app/code/Magento/Catalog/Model/Category.php @@ -780,11 +780,14 @@ public function getAllChildren($asArray = false) /** * Retrieve children ids comma separated * + * @param boolean $recursive + * @param boolean $isActive + * @param boolean $sortByPosition * @return string */ - public function getChildren() + public function getChildren($recursive = false, $isActive = true, $sortByPosition = false) { - return implode(',', $this->getResource()->getChildren($this, false)); + return implode(',', $this->getResource()->getChildren($this, $recursive, $isActive, $sortByPosition)); } /** diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php b/app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php index d7f8bd9d789a2..01e4b072b0367 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php @@ -602,9 +602,10 @@ public function isInRootCategoryList($category) * @param \Magento\Catalog\Model\Category $category * @param bool $recursive * @param bool $isActive + * @param bool $sortByPosition * @return array */ - public function getChildren($category, $recursive = true, $isActive = true) + public function getChildren($category, $recursive = true, $isActive = true, $sortByPosition = false) { $select = $this->getConnection()->select()->from( $this->getMainStoreTable($category->getStoreId()), @@ -619,6 +620,9 @@ public function getChildren($category, $recursive = true, $isActive = true) if ($isActive) { $select->where('is_active = ?', '1'); } + if ($sortByPosition) { + $select->order('position ASC'); + } $_categories = $this->getConnection()->fetchAll($select); $categoriesIds = []; foreach ($_categories as $_category) { diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTreeTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTreeTest.php index 5e9244367bb13..8eeba1230f8b1 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTreeTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTreeTest.php @@ -128,6 +128,14 @@ public function testGetChildren() $this->assertEquals(array_diff([4, 13], explode(',', $this->_model->getChildren())), []); } + public function testGetChildrenSorted() + { + $this->_model->load(2); + $unsorted = explode(',', $this->_model->getChildren()); + sort($unsorted); + $this->assertEquals(array_diff($unsorted, explode(',', $this->_model->getChildren(true, true, true))), []); + } + public function testGetPathInStore() { $this->_model->load(5);