Skip to content

Commit bd5bc6b

Browse files
author
Valeriy Nayda
authored
Merge pull request magento#753 from magento-engcom/714
714: Fatal error: Uncaught TypeError: during the saving of configurable product
2 parents 105616a + d53546c commit bd5bc6b

File tree

15 files changed

+172
-83
lines changed

15 files changed

+172
-83
lines changed

app/code/Magento/ConfigurableProduct/view/adminhtml/web/js/components/dynamic-rows-configurable.js

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,6 @@ define([
349349
);
350350

351351
_.each(data, function (row) {
352-
var attributesText;
353-
354352
if (row.productId) {
355353
index = _.indexOf(productIdsToDelete, row.productId);
356354

@@ -364,36 +362,8 @@ define([
364362
);
365363
}
366364
}
365+
product = this.getProductData(row);
367366

368-
attributesText = '';
369-
_.each(row.options, function (attribute) {
370-
if (attributesText) {
371-
attributesText += ', ';
372-
}
373-
attributesText += attribute['attribute_label'] + ': ' + attribute.label;
374-
}, this);
375-
376-
product = {
377-
'id': row.productId,
378-
'product_link': row.productUrl,
379-
'name': row.name,
380-
'sku': row.sku,
381-
'status': row.status,
382-
'price': row.price,
383-
'price_currency': row.priceCurrency,
384-
'price_string': row.priceCurrency + row.price,
385-
'weight': row.weight,
386-
'qty': row.quantity,
387-
'variationKey': row.variationKey,
388-
'configurable_attribute': row.attribute,
389-
'thumbnail_image': row.images.preview,
390-
'media_gallery': row['media_gallery'],
391-
'swatch_image': row['swatch_image'],
392-
'small_image': row['small_image'],
393-
image: row.image,
394-
'thumbnail': row.thumbnail,
395-
'attributes': attributesText
396-
};
397367
product[this.changedFlag] = true;
398368
product[this.canEditField] = row.editable;
399369
product[this.newProductField] = row.newProduct;
@@ -412,6 +382,47 @@ define([
412382
this.unionInsertData(tmpArray);
413383
},
414384

385+
/**
386+
*
387+
* @param {Object} row
388+
* @returns {Object}
389+
*/
390+
getProductData: function (row) {
391+
var product,
392+
attributesText = '';
393+
394+
_.each(row.options, function (attribute) {
395+
if (attributesText) {
396+
attributesText += ', ';
397+
}
398+
attributesText += attribute['attribute_label'] + ': ' + attribute.label;
399+
}, this);
400+
401+
product = {
402+
'id': row.productId,
403+
'product_link': row.productUrl,
404+
'name': row.name,
405+
'sku': row.sku,
406+
'status': row.status,
407+
'price': row.price,
408+
'price_currency': row.priceCurrency,
409+
'price_string': row.priceCurrency + row.price,
410+
'weight': row.weight,
411+
'qty': row.quantity,
412+
'variationKey': row.variationKey,
413+
'configurable_attribute': row.attribute,
414+
'thumbnail_image': row.images.preview,
415+
'media_gallery': row['media_gallery'],
416+
'swatch_image': row['swatch_image'],
417+
'small_image': row['small_image'],
418+
image: row.image,
419+
'thumbnail': row.thumbnail,
420+
'attributes': attributesText
421+
};
422+
423+
return product;
424+
},
425+
415426
/**
416427
* Remove array items matching condition.
417428
*

app/code/Magento/ConfigurableProduct/view/adminhtml/web/js/variations/steps/bulk.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ define([
7878
* Make options sections.
7979
*/
8080
this.makeOptionSections = function () {
81-
this.images = new self.makeImages(null);
82-
this.price = self.price;
83-
this.quantity = self.quantity;
84-
};
81+
return {
82+
images: new this.makeImages(null),
83+
price: this.price,
84+
quantity: this.quantity
85+
};
86+
}.bind(this);
8587

8688
/**
8789
* @param {Object} images
@@ -152,7 +154,7 @@ define([
152154
//fill option section data
153155
this.attributes.each(function (attribute) {
154156
attribute.chosen.each(function (option) {
155-
option.sections = ko.observable(new this.makeOptionSections());
157+
option.sections = ko.observable(this.makeOptionSections());
156158
}, this);
157159
}, this);
158160
//reset section.attribute

app/code/Magento/ConfigurableProduct/view/adminhtml/web/js/variations/steps/summary.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ define([
5353
attributes: [],
5454
attributesName: [$.mage.__('Images'), $.mage.__('SKU'), $.mage.__('Quantity'), $.mage.__('Price')],
5555
sections: [],
56-
gridTemplate: 'Magento_ConfigurableProduct/variations/steps/summary-grid'
56+
gridTemplate: 'Magento_ConfigurableProduct/variations/steps/summary-grid',
57+
quantityFieldName: 'quantity'
5758
},
5859

5960
/** @inheritdoc */
@@ -112,12 +113,12 @@ define([
112113
return memo + '-' + option.label;
113114
}, '');
114115
name = productName + _.reduce(options, function (memo, option) {
115-
return memo + '-' + option.label;
116-
}, '');
117-
quantity = getSectionValue('quantity', options);
116+
return memo + '-' + option.label;
117+
}, '');
118+
quantity = getSectionValue(this.quantityFieldName, options);
118119

119120
if (!quantity && productId) {
120-
quantity = product.quantity;
121+
quantity = product[this.quantityFieldName];
121122
}
122123
price = getSectionValue('price', options);
123124

@@ -133,12 +134,12 @@ define([
133134
images: images,
134135
sku: sku,
135136
name: name,
136-
quantity: quantity,
137137
price: price,
138138
productId: productId,
139139
weight: productWeight,
140140
editable: true
141141
};
142+
variation[this.quantityFieldName] = quantity;
142143

143144
if (productId) {
144145
variation.sku = product.sku;
@@ -163,7 +164,6 @@ define([
163164
this.variationsExisting = gridExisting;
164165
this.variationsNew = gridNew;
165166
this.variationsDeleted = gridDeleted;
166-
167167
},
168168

169169
/**
@@ -195,7 +195,7 @@ define([
195195
images: []
196196
}, variation.images));
197197
row.push(variation.sku);
198-
row.push(variation.quantity);
198+
row.push(variation[this.quantityFieldName]);
199199
_.each(variation.options, function (option) {
200200
row.push(option.label);
201201
});

app/code/Magento/InventoryConfigurableProduct/Model/GetQuantityInformationPerSource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function execute(string $sku): array
6262

6363
$sourceItemsInformation[] = [
6464
SourceItemInterface::SOURCE_CODE => $sourceItem->getSourceCode(),
65-
SourceItemInterface::QUANTITY => $sourceItem->getQuantity(),
65+
'quantity_per_source' => $sourceItem->getQuantity(),
6666
'source' => $source->getName(),
6767
];
6868
}

app/code/Magento/InventoryConfigurableProduct/Observer/ProcessSourceItemsObserver.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ public function execute(EventObserver $observer)
5151
$controller = $observer->getEvent()->getController();
5252
$configurableMatrix = $controller->getRequest()->getParam('configurable-matrix-serialized', '');
5353

54-
if ($configurableMatrix != "") {
54+
if ($configurableMatrix != '') {
5555
$productsData = json_decode($configurableMatrix, true);
5656
foreach ($productsData as $productData) {
57-
$sku = $productData[ProductInterface::SKU];
58-
$sourceItems = $productData['qty_per_source'] ?? [];
59-
60-
$this->processSourceItems($sourceItems, $sku);
57+
if (isset($productData['quantity_per_source']) && is_array($productData['quantity_per_source'])) {
58+
$sku = $productData[ProductInterface::SKU];
59+
$this->processSourceItems($productData['quantity_per_source'], $sku);
60+
}
6161
}
6262
}
6363
}
@@ -72,8 +72,9 @@ private function processSourceItems(array $sourceItems, string $productSku)
7272
{
7373
foreach ($sourceItems as $key => $sourceItem) {
7474
if (!isset($sourceItem[SourceItemInterface::STATUS])) {
75-
$sourceItems[$key][SourceItemInterface::STATUS] =
76-
$sourceItems[$key][SourceItemInterface::QUANTITY] > 0 ? 1 : 0;
75+
$sourceItems[$key][SourceItemInterface::QUANTITY] = $sourceItems[$key]['quantity_per_source'];
76+
$sourceItems[$key][SourceItemInterface::STATUS]
77+
= $sourceItems[$key][SourceItemInterface::QUANTITY] > 0 ? 1 : 0;
7778
}
7879
}
7980

app/code/Magento/InventoryConfigurableProduct/Ui/DataProvider/Product/Form/Modifier/InventoryConfigurablePanel.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ public function modifyData(array $data)
6060

6161
if (isset($data[$productId][ConfigurablePanel::CONFIGURABLE_MATRIX])) {
6262
foreach ($data[$productId][ConfigurablePanel::CONFIGURABLE_MATRIX] as $key => $productArray) {
63-
$qtyPerSource =
64-
$this->getQuantityInformationPerSource->execute($productArray[ProductInterface::SKU]);
65-
$data[$productId][ConfigurablePanel::CONFIGURABLE_MATRIX][$key]['qty_per_source'] = $qtyPerSource;
63+
$quantityPerSource
64+
= $this->getQuantityInformationPerSource->execute($productArray[ProductInterface::SKU]);
65+
$data[$productId][ConfigurablePanel::CONFIGURABLE_MATRIX][$key]['quantity_per_source']
66+
= $quantityPerSource;
6667
}
6768
}
6869
}
@@ -82,18 +83,14 @@ private function getQuantityContainerConfig(): array
8283
'data' => [
8384
'config' => [
8485
'componentType' => 'text',
85-
'component' => 'Magento_InventoryConfigurableProduct/js/form/element/quantity',
86+
'component' => 'Magento_InventoryConfigurableProduct/js/form/element/quantity-per-source',
8687
'template' => 'ui/form/field',
87-
'dataScope' => 'qty_per_source',
88+
'dataScope' => 'quantity_per_source',
8889
'label' => __('Quantity Per Source'),
8990
'formElement' => Form\Element\Input::NAME,
90-
'imports' => [
91-
'visible' => '!${$.provider}:${$.parentScope}.canEdit'
92-
],
93-
'visibleIfCanEdit' => true,
94-
]
95-
]
96-
]
91+
],
92+
],
93+
],
9794
];
9895
}
9996

@@ -110,6 +107,10 @@ public function modifyMeta(array $meta)
110107
unset($meta[ConfigurablePanel::GROUP_CONFIGURABLE]['children']
111108
[ConfigurablePanel::CONFIGURABLE_MATRIX]['children']
112109
['record']['children']['quantity_container']);
110+
111+
$meta[ConfigurablePanel::GROUP_CONFIGURABLE]['children']
112+
[ConfigurablePanel::CONFIGURABLE_MATRIX]['arguments']['data']['config']['component']
113+
= 'Magento_InventoryConfigurableProduct/js/components/dynamic-rows-configurable';
113114
}
114115

115116
return $meta;

app/code/Magento/InventoryConfigurableProduct/view/adminhtml/templates/catalog/product/edit/attribute/steps/bulk.phtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,10 +705,10 @@
705705
</div>
706706
</div>
707707

708-
<div data-bind="with: sections().quantity" class="steps-wizard-section">
708+
<div data-bind="with: sections().quantityPerSource" class="steps-wizard-section">
709709
<div data-role="section">
710710
<div class="steps-wizard-section-title">
711-
<span><?= $block->escapeHtml(__('Quantity')) ?></span>
711+
<span><?= $block->escapeHtml(__('Quantity per source')) ?></span>
712712
</div>
713713
<ul class="steps-wizard-section-list">
714714
<li>

app/code/Magento/InventoryConfigurableProduct/view/adminhtml/templates/catalog/product/edit/attribute/steps/summary.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"Magento_Ui/js/core/app": {
5353
"components": {
5454
"<?= /* @noEscape */ $block->getComponentName() ?>": {
55-
"component": "Magento_ConfigurableProduct/js/variations/steps/summary",
55+
"component": "Magento_InventoryConfigurableProduct/js/variations/steps/summary",
5656
"appendTo": "<?= /* @noEscape */ $block->getParentComponentName() ?>",
5757
"gridTemplate": "Magento_InventoryConfigurableProduct/variations/steps/summary-grid",
5858
"variationsComponent": "<?= /* @noEscape */ $block->getData('config/form') ?>.configurableVariations",

app/code/Magento/InventoryConfigurableProduct/view/adminhtml/ui_component/configurable_quantity_templates.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@
9797
<elementTmpl>ui/dynamic-rows/cells/text</elementTmpl>
9898
</settings>
9999
</field>
100-
<field name="quantity" formElement="input">
100+
<field name="quantity_per_source" formElement="input">
101101
<settings>
102-
<label translate="true">Quantity</label>
102+
<label translate="true">Quantity Per Source</label>
103103
<dataType>text</dataType>
104104
<validation>
105105
<rule name="required-entry" xsi:type="boolean">true</rule>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
define([
7+
'Magento_ConfigurableProduct/js/components/dynamic-rows-configurable'
8+
], function (dynamicRowsConfigurable) {
9+
'use strict';
10+
11+
return dynamicRowsConfigurable.extend({
12+
defaults: {
13+
quantityFieldName: 'quantity_per_source'
14+
},
15+
16+
/** @inheritdoc */
17+
getProductData: function (row) {
18+
var product = this._super(row);
19+
20+
product[this.quantityFieldName] = row.quantityPerSource;
21+
22+
return product;
23+
}
24+
});
25+
});

0 commit comments

Comments
 (0)