8
8
9
9
use Magento \Catalog \Api \Data \ProductCustomOptionInterface ;
10
10
use Magento \Catalog \Api \Data \ProductCustomOptionValuesInterface ;
11
+ use Magento \Catalog \Api \Data \ProductCustomOptionValuesInterfaceFactory ;
11
12
use Magento \Catalog \Api \Data \ProductInterface ;
12
13
use Magento \Catalog \Model \Product ;
13
14
use Magento \Catalog \Model \ResourceModel \Product \Option \Value \Collection ;
@@ -102,6 +103,11 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
102
103
*/
103
104
private $ metadataPool ;
104
105
106
+ /**
107
+ * @var ProductCustomOptionValuesInterfaceFactory
108
+ */
109
+ private $ customOptionValuesFactory ;
110
+
105
111
/**
106
112
* @param \Magento\Framework\Model\Context $context
107
113
* @param \Magento\Framework\Registry $registry
@@ -114,6 +120,7 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
114
120
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
115
121
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
116
122
* @param array $data
123
+ * @param ProductCustomOptionValuesInterfaceFactory|null $customOptionValuesFactory
117
124
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
118
125
*/
119
126
public function __construct (
@@ -127,12 +134,16 @@ public function __construct(
127
134
Option \Validator \Pool $ validatorPool ,
128
135
\Magento \Framework \Model \ResourceModel \AbstractResource $ resource = null ,
129
136
\Magento \Framework \Data \Collection \AbstractDb $ resourceCollection = null ,
130
- array $ data = []
137
+ array $ data = [],
138
+ ProductCustomOptionValuesInterfaceFactory $ customOptionValuesFactory = null
131
139
) {
132
140
$ this ->productOptionValue = $ productOptionValue ;
133
141
$ this ->optionTypeFactory = $ optionFactory ;
134
142
$ this ->validatorPool = $ validatorPool ;
135
143
$ this ->string = $ string ;
144
+ $ this ->customOptionValuesFactory = $ customOptionValuesFactory ?:
145
+ \Magento \Framework \App \ObjectManager::getInstance ()->get (ProductCustomOptionValuesInterfaceFactory::class);
146
+
136
147
parent ::__construct (
137
148
$ context ,
138
149
$ registry ,
@@ -391,20 +402,21 @@ public function beforeSave()
391
402
*/
392
403
public function afterSave ()
393
404
{
394
- $ this ->getValueInstance ()->unsetValues ();
395
405
$ values = $ this ->getValues () ?: $ this ->getData ('values ' );
396
406
if (is_array ($ values )) {
397
407
foreach ($ values as $ value ) {
398
- if ($ value instanceof \ Magento \ Catalog \ Api \ Data \ ProductCustomOptionValuesInterface) {
408
+ if ($ value instanceof ProductCustomOptionValuesInterface) {
399
409
$ data = $ value ->getData ();
400
410
} else {
401
411
$ data = $ value ;
402
412
}
403
- $ this ->getValueInstance ()->addValue ($ data );
404
- }
405
413
406
- $ this ->getValueInstance ()->setOption ($ this )->saveValues ();
407
- } elseif ($ this ->getGroupByType ($ this ->getType ()) == self ::OPTION_GROUP_SELECT ) {
414
+ $ this ->customOptionValuesFactory ->create ()
415
+ ->addValue ($ data )
416
+ ->setOption ($ this )
417
+ ->saveValues ();
418
+ }
419
+ } elseif ($ this ->getGroupByType ($ this ->getType ()) === self ::OPTION_GROUP_SELECT ) {
408
420
throw new LocalizedException (__ ('Select type options required values rows. ' ));
409
421
}
410
422
@@ -805,7 +817,7 @@ public function setImageSizeY($imageSizeY)
805
817
}
806
818
807
819
/**
808
- * @param \Magento\Catalog\Api\Data\ ProductCustomOptionValuesInterface[] $values
820
+ * @param ProductCustomOptionValuesInterface[] $values
809
821
* @return $this
810
822
*/
811
823
public function setValues (array $ values = null )
0 commit comments