Skip to content

Commit 2a68f5d

Browse files
committed
MAGETWO-61960: Fails to save configurable product with specific product option
1 parent 360b173 commit 2a68f5d

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

app/code/Magento/Catalog/view/adminhtml/templates/catalog/product/edit.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
<div data-role="tabs" id="product-edit-form-tabs"></div> <?php /* @TODO: remove id after elimination of setDestElementId('product-edit-form-tabs') */?>
5454
<?php echo $block->getChildHtml('product-type-tabs') ?>
5555
<input type="hidden" id="product_type_id" value="<?php /* @escapeNotVerified */ echo $block->getProduct()->getTypeId()?>"/>
56-
<input type="hidden" id="attribute_set_id" value="<?php /* @escapeNotVerified */ echo $block->getProduct()->getAttributeSetId()?>"/>
56+
<input type="hidden" id="attribute_set_id" name="set" value="<?php /* @escapeNotVerified */ echo $block->getProduct()->getAttributeSetId()?>"/>
5757
<button type="submit" class="hidden"></button>
5858
<?php if ($block->getUseContainer()): ?>
5959
</form>

app/code/Magento/ConfigurableProduct/Controller/Adminhtml/Product/Initialization/Helper/Plugin/Configurable.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ public function afterInitialize(
5151
$attributes = $this->request->getParam('attributes');
5252
if ($product->getTypeId() == ConfigurableProduct::TYPE_CODE && !empty($attributes)) {
5353
$setId = $this->request->getPost('new-variations-attribute-set-id');
54+
55+
if (!$setId) {
56+
$setId = $this->request->getPost('set');
57+
}
5458
$product->setAttributeSetId($setId);
59+
$product->getResource()->getSortedAttributes($setId);
5560
$this->productType->setUsedProductAttributeIds($attributes, $product);
5661

5762
$product->setNewVariationsAttributeSetId($setId);

app/code/Magento/ConfigurableProduct/Controller/Adminhtml/Product/Initialization/Helper/Plugin/UpdateConfigurations.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,19 @@ public function afterInitialize(
5151
$configurations = json_decode($this->request->getParam('configurations_serialized'), true);
5252
}
5353
$configurations = $this->variationHandler->duplicateImagesForVariations($configurations);
54+
$productsAttributeSets = [];
55+
5456
foreach ($configurations as $productId => $productData) {
5557
/** @var \Magento\Catalog\Model\Product $product */
5658
$product = $this->productRepository->getById($productId, false, $this->request->getParam('store', 0));
5759
$productData = $this->variationHandler->processMediaGallery($product, $productData);
60+
$productAttributeSetId = $product->getAttributeSetId();
61+
62+
if (!array_key_exists($productAttributeSetId, $productsAttributeSets)) {
63+
$product->getResource()->getSortedAttributes($productAttributeSetId);
64+
$productsAttributeSets[$productAttributeSetId] = 1;
65+
}
66+
5867
$product->addData($productData);
5968
if ($product->hasDataChanges()) {
6069
$product->save();

app/code/Magento/ConfigurableProduct/Test/Unit/Controller/Adminhtml/Product/Initialization/Helper/Plugin/ConfigurableTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\ConfigurableProduct\Test\Unit\Controller\Adminhtml\Product\Initialization\Helper\Plugin;
77

8+
use Magento\Catalog\Model\ResourceModel\Product;
89
use \Magento\ConfigurableProduct\Controller\Adminhtml\Product\Initialization\Helper\Plugin\Configurable;
910
use Magento\ConfigurableProduct\Model\Product\Type\Configurable as ConfigurableProduct;
1011

@@ -62,6 +63,7 @@ protected function setUp()
6263
'setAssociatedProductIds',
6364
'setCanSaveConfigurableAttributes',
6465
'getTypeId',
66+
'getResource',
6567
'__wakeup',
6668
];
6769
$this->productMock = $this->getMock('Magento\Catalog\Model\Product', $methods, [], '', false);
@@ -77,13 +79,16 @@ protected function setUp()
7779

7880
public function testAfterInitializeIfAttributesNotEmptyAndActionNameNotGenerateVariations()
7981
{
82+
$postValue = 'postValue';
83+
$productResourceMock = $this->getProductResource($postValue);
84+
8085
$this->productMock->expects($this->once())->method('getTypeId')->willReturn(ConfigurableProduct::TYPE_CODE);
86+
$this->productMock->expects($this->once())->method('getResource')->willReturn($productResourceMock);
8187
$associatedProductIds = ['key' => 'value'];
8288
$associatedProductIdsSerialized = json_encode($associatedProductIds);
8389
$generatedProductIds = ['key_one' => 'value_one'];
8490
$expectedArray = ['key' => 'value', 'key_one' => 'value_one'];
8591
$attributes = ['key' => 'value'];
86-
$postValue = 'postValue';
8792
$variationsMatrix = ['variationKey' => 'variationValue'];
8893
$variationsMatrixSerialized = json_encode($variationsMatrix);
8994

@@ -125,11 +130,13 @@ public function testAfterInitializeIfAttributesNotEmptyAndActionNameNotGenerateV
125130

126131
public function testAfterInitializeIfAttributesNotEmptyAndActionNameGenerateVariations()
127132
{
133+
$postValue = 'postValue';
134+
$productResourceMock = $this->getProductResource($postValue);
128135
$this->productMock->expects($this->once())->method('getTypeId')->willReturn(ConfigurableProduct::TYPE_CODE);
136+
$this->productMock->expects($this->once())->method('getResource')->willReturn($productResourceMock);
129137
$associatedProductIds = ['key' => 'value'];
130138
$associatedProductIdsSerialized = json_encode($associatedProductIds);
131139
$attributes = ['key' => 'value'];
132-
$postValue = 'postValue';
133140
$valueMap = [
134141
['new-variations-attribute-set-id', null, $postValue],
135142
['associated_product_ids_serialized', '[]', $associatedProductIdsSerialized],
@@ -182,4 +189,16 @@ public function testAfterInitializeForNotConfigurableProduct()
182189
$this->productTypeMock->expects($this->never())->method('generateSimpleProducts');
183190
$this->plugin->afterInitialize($this->subjectMock, $this->productMock);
184191
}
192+
193+
private function getProductResource($postValue)
194+
{
195+
$productResourceMock = $this->getMockBuilder(Product::class)
196+
->disableOriginalConstructor()
197+
->getMock();
198+
$productResourceMock->expects(self::once())
199+
->method('getSortedAttributes')
200+
->with($postValue);
201+
202+
return $productResourceMock;
203+
}
185204
}

app/code/Magento/ConfigurableProduct/view/adminhtml/templates/product/configurable/affected-attribute-set-selector/js.phtml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,11 @@
164164
$('[data-role=new-variations-attribute-set-id]').val($('#attribute_set_id').val());
165165
return; // all selected configurable attributes belong to current attribute set
166166
}
167-
if (!$('[data-role=product-variations-matrix] [data-column=entity_id]')
168-
.closest('tr').has('input[name$="[name]"]').length
169-
) {
170-
return; // no new simple products to save from matrix: uniting attribute set is not needed
167+
168+
var regex = /id\/(\d+)(?:\/)?/g;
169+
var existingProductPage = regex.exec(BASE_URL);
170+
if (existingProductPage != null) {
171+
return; // it is not new product page
171172
}
172173

173174
event.stopImmediatePropagation();

0 commit comments

Comments
 (0)