forked from ashishjob0424/magento2-patches
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Patch-Magento_Catalog-M2.1.14EE-custom-option-id-regeneration-on-save.patch
108 lines (103 loc) · 4.29 KB
/
Patch-Magento_Catalog-M2.1.14EE-custom-option-id-regeneration-on-save.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
diff --git a/module-catalog/Model/Product/Option/Repository.php b/module-catalog/Model/Product/Option/Repository.php
index 579eaec..843e1c3 100644
--- a/module-catalog/Model/Product/Option/Repository.php
+++ b/module-catalog/Model/Product/Option/Repository.php
@@ -179,7 +179,13 @@ class Repository implements \Magento\Catalog\Api\ProductCustomOptionRepositoryIn
/** @var array $originalValues */
$originalValues = $persistedOption->getValues();
/** @var array $newValues */
- $newValues = $option->getData('values');
+ $newValues = [];
+ foreach($option->getData('values') as $newValue){
+ if (is_null($newValue['option_type_id'])){
+ $newValues[] = $newValue;
+ }
+ }
+
if ($newValues) {
if (isset($originalValues)) {
$newValues = $this->markRemovedValues($newValues, $originalValues);
diff --git a/module-catalog/Model/Product/Option/SaveHandler.php b/module-catalog/Model/Product/Option/SaveHandler.php
index ff15f88..665d064 100644
--- a/module-catalog/Model/Product/Option/SaveHandler.php
+++ b/module-catalog/Model/Product/Option/SaveHandler.php
@@ -3,9 +3,10 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
+
namespace Magento\Catalog\Model\Product\Option;
-use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface as OptionRepository;
+use Magento\Catalog\Model\Product\Option\Repository as OptionRepository;
use Magento\Framework\EntityManager\Operation\ExtensionInterface;
/**
@@ -23,7 +24,8 @@ class SaveHandler implements ExtensionInterface
*/
public function __construct(
OptionRepository $optionRepository
- ) {
+ )
+ {
$this->optionRepository = $optionRepository;
}
@@ -35,17 +37,28 @@ class SaveHandler implements ExtensionInterface
*/
public function execute($entity, $arguments = [])
{
+ $origOptions = $entity->getOrigData()['options'];
+
$options = $entity->getOptions();
+
+ $map = [];
$optionIds = [];
- if ($options) {
- $optionIds = array_map(
- function ($option) {
- /** @var \Magento\Catalog\Model\Product\Option $option */
- return $option->getOptionId();
- },
- $options
- );
+ if (!empty($origOptions)) {
+ //Map already existing options
+ foreach ($origOptions as $origOption) {
+ $values = array();
+ foreach ($origOption->getValues() as $value) {
+ $values[$value->getData('mageworx_option_type_id')] = $value->getOptionTypeId();
+ }
+
+ $map[$origOption->getSku()] = [
+ 'option_id' => $origOption->getOptionId(),
+ 'values' => $values
+ ];
+
+ $optionIds[] = $origOption->getOptionId();
+ }
}
/** @var \Magento\Catalog\Api\Data\ProductInterface $entity */
@@ -54,9 +67,24 @@ class SaveHandler implements ExtensionInterface
$this->optionRepository->delete($option);
}
}
-
if ($options) {
foreach ($options as $option) {
+ if (is_null($option->getOptionId()) && array_key_exists($option->getSku(), $map)) {
+ $option->setOptionId($map[$option->getSku()]['option_id']);
+ }
+ $optionDataValues = $option->getData('values');
+ $refactoredValues = [];
+ foreach ($optionDataValues as $val) {
+ if (isset($val['mageworx_option_type_id']) && is_null($val['option_type_id']) && array_key_exists($val['mageworx_option_type_id'], $map[$option->getSku()]['values'])) {
+ $val['option_type_id'] = $map[$option->getSku()]['values'][$val['mageworx_option_type_id']];
+ }
+ $refactoredValues[] = $val;
+ }
+
+ $option->unsetData('values');
+ $option->setData('values', $refactoredValues);
+
+ $option->setProductSku($entity->getSku());
$this->optionRepository->save($option);
}
}