Skip to content

Commit 107c91e

Browse files
committed
Merge remote-tracking branch 'tango-ce/MAGETWO-52172' into MAGETWO-52578
2 parents feab3b9 + 4affdce commit 107c91e

File tree

4 files changed

+112
-23
lines changed

4 files changed

+112
-23
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
163163
} else {
164164
$productOptions = [];
165165
}
166+
166167
$product->addData($productData);
167168

168169
if ($wasLockedMedia) {
@@ -307,20 +308,28 @@ protected function normalize(array $productData)
307308
public function mergeProductOptions($productOptions, $overwriteOptions)
308309
{
309310
if (!is_array($productOptions)) {
310-
$productOptions = [];
311+
return [];
311312
}
312-
if (is_array($overwriteOptions)) {
313-
$options = array_replace_recursive($productOptions, $overwriteOptions);
314-
array_walk_recursive($options, function (&$item) {
315-
if ($item === "") {
316-
$item = null;
313+
314+
if (!is_array($overwriteOptions)) {
315+
return $productOptions;
316+
}
317+
318+
foreach ($productOptions as $index => $option) {
319+
$optionId = $option['option_id'];
320+
321+
if (!isset($overwriteOptions[$optionId])) {
322+
continue;
323+
}
324+
325+
foreach ($overwriteOptions[$optionId] as $fieldName => $overwrite) {
326+
if ($overwrite && isset($option[$fieldName]) && isset($option['default_' . $fieldName])) {
327+
$productOptions[$index][$fieldName] = $option['default_' . $fieldName];
317328
}
318-
});
319-
} else {
320-
$options = $productOptions;
329+
}
321330
}
322331

323-
return $options;
332+
return $productOptions;
324333
}
325334

326335
/**

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Initialization/HelperTest.php

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -326,25 +326,72 @@ public function testInitialize()
326326
public function mergeProductOptionsDataProvider()
327327
{
328328
return [
329-
[
329+
'options are not array, empty array is returned' => [
330330
null,
331331
[],
332332
[],
333333
],
334-
[
335-
['key' => 'val'],
334+
'replacement is not array, original options are returned' => [
335+
['val'],
336336
null,
337-
['key' => 'val'],
337+
['val'],
338338
],
339-
[
340-
['key' => ['key' => 'val']],
341-
['key' => ['key' => 'val2', 'key2' => 'val2']],
342-
['key' => ['key' => 'val2', 'key2' => 'val2']],
339+
'ids do not match, no replacement occurs' => [
340+
[
341+
[
342+
'option_id' => '3',
343+
'key1' => 'val1',
344+
'default_key1' => 'val2'
345+
]
346+
],
347+
[4 => ['key1' => '1']],
348+
[
349+
[
350+
'option_id' => '3',
351+
'key1' => 'val1',
352+
'default_key1' => 'val2'
353+
]
354+
]
343355
],
344-
[
345-
['key' => ['key' => 'val', 'another_key' => 'another_value']],
346-
['key' => ['key' => 'val2', 'key2' => 'val2']],
347-
['key' => ['key' => 'val2', 'another_key' => 'another_value', 'key2' => 'val2',]],
356+
'key2 is replaced, key1 is not (checkbox is not checked)' => [
357+
[
358+
[
359+
'option_id' => '5',
360+
'key1' => 'val1',
361+
'key2' => 'val2',
362+
'default_key1' => 'val3',
363+
'default_key2' => 'val4'
364+
]
365+
],
366+
[5 => ['key1' => '0', 'key2' => '1']],
367+
[
368+
[
369+
'option_id' => '5',
370+
'key1' => 'val1',
371+
'key2' => 'val4',
372+
'default_key1' => 'val3',
373+
'default_key2' => 'val4'
374+
]
375+
]
376+
],
377+
'key1 is replaced, key2 has no default value' => [
378+
[
379+
[
380+
'option_id' => '7',
381+
'key1' => 'val1',
382+
'key2' => 'val2',
383+
'default_key1' => 'val3'
384+
]
385+
],
386+
[7 => ['key1' => '1', 'key2' => '1']],
387+
[
388+
[
389+
'option_id' => '7',
390+
'key1' => 'val3',
391+
'key2' => 'val2',
392+
'default_key1' => 'val3'
393+
]
394+
],
348395
],
349396
];
350397
}

app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/CustomOptions.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ protected function getImportOptionsModalConfig()
497497
*/
498498
protected function getCommonContainerConfig($sortOrder)
499499
{
500-
return [
500+
$commonContainer = [
501501
'arguments' => [
502502
'data' => [
503503
'config' => [
@@ -522,6 +522,9 @@ protected function getCommonContainerConfig($sortOrder)
522522
'label' => __('Option Title'),
523523
'component' => 'Magento_Catalog/component/static-type-input',
524524
'valueUpdate' => 'input',
525+
'imports' => [
526+
'optionId' => '${ $.provider }:${ $.parentScope }.option_id'
527+
]
525528
],
526529
],
527530
],
@@ -531,6 +534,19 @@ protected function getCommonContainerConfig($sortOrder)
531534
static::FIELD_IS_REQUIRE_NAME => $this->getIsRequireFieldConfig(40)
532535
]
533536
];
537+
538+
if ($this->locator->getProduct()->getStoreId()) {
539+
$useDefaultConfig = [
540+
'service' => [
541+
'template' => 'Magento_Catalog/form/element/helper/custom-option-service',
542+
]
543+
];
544+
$titlePath = $this->arrayManager->findPath(static::FIELD_TITLE_NAME, $commonContainer, null)
545+
. static::META_CONFIG_PATH;
546+
$commonContainer = $this->arrayManager->merge($titlePath, $commonContainer, $useDefaultConfig);
547+
}
548+
549+
return $commonContainer;
534550
}
535551

536552
/**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!--
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<div class="admin__field-service">
8+
<input type="checkbox"
9+
class="admin__control-checkbox"
10+
attr="
11+
id: $data.uid + '_default',
12+
name: 'options_use_default[' + $data.optionId + '][' + $data.index + ']',
13+
'data-form-part': $data.ns
14+
"
15+
ko-checked="isUseDefault">
16+
<label translate="'Use Default Value'" attr="for: $data.uid + '_default'" class="admin__field-label"></label>
17+
</div>

0 commit comments

Comments
 (0)