88
99use Magento \Catalog \Api \Data \ProductCustomOptionInterface ;
1010use Magento \Catalog \Api \Data \ProductCustomOptionValuesInterface ;
11+ use Magento \Catalog \Api \Data \ProductCustomOptionValuesInterfaceFactory ;
1112use Magento \Catalog \Api \Data \ProductInterface ;
1213use Magento \Catalog \Model \Product ;
1314use Magento \Catalog \Model \ResourceModel \Product \Option \Value \Collection ;
@@ -102,6 +103,11 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
102103 */
103104 private $ metadataPool ;
104105
106+ /**
107+ * @var ProductCustomOptionValuesInterfaceFactory
108+ */
109+ private $ customOptionValuesFactory ;
110+
105111 /**
106112 * @param \Magento\Framework\Model\Context $context
107113 * @param \Magento\Framework\Registry $registry
@@ -114,6 +120,7 @@ class Option extends AbstractExtensibleModel implements ProductCustomOptionInter
114120 * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
115121 * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
116122 * @param array $data
123+ * @param ProductCustomOptionValuesInterfaceFactory|null $customOptionValuesFactory
117124 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
118125 */
119126 public function __construct (
@@ -127,12 +134,16 @@ public function __construct(
127134 Option \Validator \Pool $ validatorPool ,
128135 \Magento \Framework \Model \ResourceModel \AbstractResource $ resource = null ,
129136 \Magento \Framework \Data \Collection \AbstractDb $ resourceCollection = null ,
130- array $ data = []
137+ array $ data = [],
138+ ProductCustomOptionValuesInterfaceFactory $ customOptionValuesFactory = null
131139 ) {
132140 $ this ->productOptionValue = $ productOptionValue ;
133141 $ this ->optionTypeFactory = $ optionFactory ;
134142 $ this ->validatorPool = $ validatorPool ;
135143 $ this ->string = $ string ;
144+ $ this ->customOptionValuesFactory = $ customOptionValuesFactory ?:
145+ \Magento \Framework \App \ObjectManager::getInstance ()->get (ProductCustomOptionValuesInterfaceFactory::class);
146+
136147 parent ::__construct (
137148 $ context ,
138149 $ registry ,
@@ -390,20 +401,21 @@ public function beforeSave()
390401 */
391402 public function afterSave ()
392403 {
393- $ this ->getValueInstance ()->unsetValues ();
394404 $ values = $ this ->getValues () ?: $ this ->getData ('values ' );
395405 if (is_array ($ values )) {
396406 foreach ($ values as $ value ) {
397- if ($ value instanceof \ Magento \ Catalog \ Api \ Data \ ProductCustomOptionValuesInterface) {
407+ if ($ value instanceof ProductCustomOptionValuesInterface) {
398408 $ data = $ value ->getData ();
399409 } else {
400410 $ data = $ value ;
401411 }
402- $ this ->getValueInstance ()->addValue ($ data );
403- }
404412
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 ) {
407419 throw new LocalizedException (__ ('Select type options required values rows. ' ));
408420 }
409421
@@ -804,7 +816,7 @@ public function setImageSizeY($imageSizeY)
804816 }
805817
806818 /**
807- * @param \Magento\Catalog\Api\Data\ ProductCustomOptionValuesInterface[] $values
819+ * @param ProductCustomOptionValuesInterface[] $values
808820 * @return $this
809821 */
810822 public function setValues (array $ values = null )
0 commit comments