Skip to content

Commit 071ba86

Browse files
author
Valeriy Nayda
committed
MAGETWO-50409: Changing order of configurable attributes is ignored on frontend
1 parent bc7c2fd commit 071ba86

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

app/code/Magento/ConfigurableProduct/Model/ConfigurableAttributeData.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ public function getAttributesData(Product $product, array $options = [])
3030
if ($attributeOptionsData) {
3131
$productAttribute = $attribute->getProductAttribute();
3232
$attributeId = $productAttribute->getId();
33-
$attributes[] = [
33+
$attributes[$attributeId] = [
3434
'id' => $attributeId,
3535
'code' => $productAttribute->getAttributeCode(),
3636
'label' => $productAttribute->getStoreLabel($product->getStoreId()),
3737
'options' => $attributeOptionsData,
38+
'position' => $attribute->getPosition(),
3839
];
3940
$defaultValues[$attributeId] = $this->getAttributeConfigValue($attributeId, $product);
4041
}

app/code/Magento/ConfigurableProduct/Test/Unit/Model/ConfigurableAttributeDataTest.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@ public function testPrepareJsonAttributes()
6767
['value_index' => 'option_id_1', 'label' => 'label_1'],
6868
['value_index' => 'option_id_2', 'label' => 'label_2'],
6969
];
70+
$position = 2;
7071
$expected = [
7172
'attributes' => [
7273
$attributeId => [
7374
'id' => $attributeId,
7475
'code' => 'test_attribute',
7576
'label' => 'Test',
77+
'position' => $position,
7678
'options' => [
7779
0 => [
7880
'id' => 'option_id_1',
@@ -108,11 +110,15 @@ public function testPrepareJsonAttributes()
108110

109111
$attributeMock = $this->getMockBuilder('Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute')
110112
->disableOriginalConstructor()
111-
->setMethods(['getProductAttribute', '__wakeup', 'getLabel', 'getOptions', 'getAttributeId'])
113+
->setMethods(['getProductAttribute', '__wakeup', 'getLabel', 'getOptions', 'getAttributeId', 'getPosition'])
112114
->getMock();
113115
$attributeMock->expects($this->once())
114116
->method('getProductAttribute')
115117
->willReturn($productAttributeMock);
118+
$attributeMock->expects($this->once())
119+
->method('getPosition')
120+
->willReturn($position);
121+
116122
$this->product->expects($this->once())->method('getStoreId')->willReturn($storeId);
117123
$productAttributeMock->expects($this->once())
118124
->method('getStoreLabel')

app/code/Magento/Swatches/view/frontend/web/js/swatch-renderer.js

+10
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,22 @@ define([
205205
*/
206206
_init: function () {
207207
if (this.options.jsonConfig !== '' && this.options.jsonSwatchConfig !== '') {
208+
this._sortAttributes();
208209
this._RenderControls();
209210
} else {
210211
console.log('SwatchRenderer: No input data received');
211212
}
212213
},
213214

215+
/**
216+
* @private
217+
*/
218+
_sortAttributes: function () {
219+
this.options.jsonConfig.attributes = _.sortBy(this.options.jsonConfig.attributes, function (attribute) {
220+
return attribute.position;
221+
});
222+
},
223+
214224
/**
215225
* @private
216226
*/

0 commit comments

Comments
 (0)