Skip to content

Commit 83a8ef5

Browse files
author
Oleksii Korshenko
authored
MAGETWO-81456: ADDED $sortByPostion flag to getChildren() #11342
2 parents b7be473 + 0c0e6ef commit 83a8ef5

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,11 +782,14 @@ public function getAllChildren($asArray = false)
782782
/**
783783
* Retrieve children ids comma separated
784784
*
785+
* @param boolean $recursive
786+
* @param boolean $isActive
787+
* @param boolean $sortByPosition
785788
* @return string
786789
*/
787-
public function getChildren()
790+
public function getChildren($recursive = false, $isActive = true, $sortByPosition = false)
788791
{
789-
return implode(',', $this->getResource()->getChildren($this, false));
792+
return implode(',', $this->getResource()->getChildren($this, $recursive, $isActive, $sortByPosition));
790793
}
791794

792795
/**

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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 8 additions & 0 deletions
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)