From 2e83a36fbf3201bdaf6612dcfce08125687f0e28 Mon Sep 17 00:00:00 2001 From: Magento EngCom Team Date: Tue, 23 Jan 2018 10:54:15 -0600 Subject: [PATCH] :arrow_double_up: Forwardport of magento/magento2#11342 to 2.3-develop branch Applied pull request patch https://github.com/magento/magento2/pull/11342.patch (created by @denisristic) based on commit(s): 1. 6dfb610d611f6b22770f0f738951bdb89ca73734 2. f77dadbd3ad92610515b7fecd22f6217bb708df5 3. fc4723441c7652917201cac2b4411639bf2acd76 4. 9d593a4dbd55d6bd0343c3587c1b9bc3e1470d5e 5. 79693c8800f61780bcd44cf3fbbee92615f70514 Fixed GitHub Issues in 2.3-develop branch: - magento/magento2#11310: Method "getChildren" sort ordering (reported by @EliasKotlyar) --- app/code/Magento/Catalog/Model/Category.php | 7 +++++-- .../Magento/Catalog/Model/ResourceModel/Category/Flat.php | 6 +++++- .../testsuite/Magento/Catalog/Model/CategoryTreeTest.php | 8 ++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) 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);