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 ,
@@ -390,20 +401,21 @@ public function beforeSave()
390
401
*/
391
402
public function afterSave ()
392
403
{
393
- $ this ->getValueInstance ()->unsetValues ();
394
404
$ values = $ this ->getValues () ?: $ this ->getData ('values ' );
395
405
if (is_array ($ values )) {
396
406
foreach ($ values as $ value ) {
397
- if ($ value instanceof \ Magento \ Catalog \ Api \ Data \ ProductCustomOptionValuesInterface) {
407
+ if ($ value instanceof ProductCustomOptionValuesInterface) {
398
408
$ data = $ value ->getData ();
399
409
} else {
400
410
$ data = $ value ;
401
411
}
402
- $ this ->getValueInstance ()->addValue ($ data );
403
- }
404
412
405
- $ this ->getValueInstance ()->setOption ($ this )->saveValues ();
406
- } elseif ($ this ->getGroupByType ($ this ->getType ()) == self ::OPTION_GROUP_SELECT ) {
413
+ $ this ->customOptionValuesFactory ->create ()
414
+ ->addValue ($ data )
415
+ ->setOption ($ this )
416
+ ->saveValues ();
417
+ }
418
+ } elseif ($ this ->getGroupByType ($ this ->getType ()) === self ::OPTION_GROUP_SELECT ) {
407
419
throw new LocalizedException (__ ('Select type options required values rows. ' ));
408
420
}
409
421
@@ -804,7 +816,7 @@ public function setImageSizeY($imageSizeY)
804
816
}
805
817
806
818
/**
807
- * @param \Magento\Catalog\Api\Data\ ProductCustomOptionValuesInterface[] $values
819
+ * @param ProductCustomOptionValuesInterface[] $values
808
820
* @return $this
809
821
*/
810
822
public function setValues (array $ values = null )
0 commit comments