Skip to content

Commit 2e83a36

Browse files
⏫ Forwardport of #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. 6dfb610 2. f77dadb 3. fc47234 4. 9d593a4 5. 79693c8 Fixed GitHub Issues in 2.3-develop branch: - #11310: Method "getChildren" sort ordering (reported by @EliasKotlyar)
1 parent 8e77e2f commit 2e83a36

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

app/code/Magento/Catalog/Model/Category.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -780,11 +780,14 @@ public function getAllChildren($asArray = false)
780780
/**
781781
* Retrieve children ids comma separated
782782
*
783+
* @param boolean $recursive
784+
* @param boolean $isActive
785+
* @param boolean $sortByPosition
783786
* @return string
784787
*/
785-
public function getChildren()
788+
public function getChildren($recursive = false, $isActive = true, $sortByPosition = false)
786789
{
787-
return implode(',', $this->getResource()->getChildren($this, false));
790+
return implode(',', $this->getResource()->getChildren($this, $recursive, $isActive, $sortByPosition));
788791
}
789792

790793
/**

app/code/Magento/Catalog/Model/ResourceModel/Category/Flat.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,10 @@ public function isInRootCategoryList($category)
602602
* @param \Magento\Catalog\Model\Category $category
603603
* @param bool $recursive
604604
* @param bool $isActive
605+
* @param bool $sortByPosition
605606
* @return array
606607
*/
607-
public function getChildren($category, $recursive = true, $isActive = true)
608+
public function getChildren($category, $recursive = true, $isActive = true, $sortByPosition = false)
608609
{
609610
$select = $this->getConnection()->select()->from(
610611
$this->getMainStoreTable($category->getStoreId()),
@@ -619,6 +620,9 @@ public function getChildren($category, $recursive = true, $isActive = true)
619620
if ($isActive) {
620621
$select->where('is_active = ?', '1');
621622
}
623+
if ($sortByPosition) {
624+
$select->order('position ASC');
625+
}
622626
$_categories = $this->getConnection()->fetchAll($select);
623627
$categoriesIds = [];
624628
foreach ($_categories as $_category) {

dev/tests/integration/testsuite/Magento/Catalog/Model/CategoryTreeTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ public function testGetChildren()
128128
$this->assertEquals(array_diff([4, 13], explode(',', $this->_model->getChildren())), []);
129129
}
130130

131+
public function testGetChildrenSorted()
132+
{
133+
$this->_model->load(2);
134+
$unsorted = explode(',', $this->_model->getChildren());
135+
sort($unsorted);
136+
$this->assertEquals(array_diff($unsorted, explode(',', $this->_model->getChildren(true, true, true))), []);
137+
}
138+
131139
public function testGetPathInStore()
132140
{
133141
$this->_model->load(5);

0 commit comments

Comments
 (0)