Skip to content

Commit eaa0617

Browse files
ENGCOM-6489: #24357 Eav sort order by attribute option_id #24360
2 parents 8181457 + 2f9d297 commit eaa0617

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

app/code/Magento/Eav/Model/Entity/Attribute/Source/Table.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,13 @@ public function addValueSortToCollection($collection, $dir = \Magento\Framework\
211211
$collection,
212212
$attribute,
213213
$valueExpr
214+
)->addOptionToCollection(
215+
$collection,
216+
$attribute,
217+
$valueExpr
214218
);
215219

216-
$collection->getSelect()->order("{$attribute->getAttributeCode()}_value {$dir}");
220+
$collection->getSelect()->order("{$attribute->getAttributeCode()}_order {$dir}");
217221

218222
return $this;
219223
}

app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute/Option.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,33 @@ public function addOptionValueToCollection($collection, $attribute, $valueExpr)
6767
return $this;
6868
}
6969

70+
/**
71+
* Add Join with option for collection select
72+
*
73+
* @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection
74+
* @param \Magento\Eav\Model\Entity\Attribute $attribute
75+
* @param \Zend_Db_Expr $valueExpr
76+
* @return $this
77+
*/
78+
public function addOptionToCollection($collection, $attribute, $valueExpr)
79+
{
80+
$connection = $this->getConnection();
81+
$attributeCode = $attribute->getAttributeCode();
82+
$optionTable1 = $attributeCode . '_option_t1';
83+
$tableJoinCond1 = "{$optionTable1}.option_id={$valueExpr}";
84+
$valueExpr = $connection->getIfNullSql(
85+
"{$optionTable1}.sort_order"
86+
);
87+
88+
$collection->getSelect()->joinLeft(
89+
[$optionTable1 => $this->getTable('eav_attribute_option')],
90+
$tableJoinCond1,
91+
["{$attributeCode}_order" => $valueExpr]
92+
);
93+
94+
return $this;
95+
}
96+
7097
/**
7198
* Retrieve Select for update Flat data
7299
*

app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/Source/TableTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,10 @@ public function testAddValueSortToCollection()
314314
$attrOption->expects($this->once())->method('addOptionValueToCollection')
315315
->with($collection, $this->abstractAttributeMock, $expr)
316316
->willReturnSelf();
317-
$select->expects($this->once())->method('order')->with("{$attributeCode}_value {$dir}");
317+
$attrOption->expects($this->once())->method('addOptionToCollection')
318+
->with($collection, $this->abstractAttributeMock, $expr)
319+
->willReturnSelf();
320+
$select->expects($this->once())->method('order')->with("{$attributeCode}_order {$dir}");
318321

319322
$this->assertEquals($this->model, $this->model->addValueSortToCollection($collection, $dir));
320323
}

0 commit comments

Comments
 (0)