Skip to content

Commit 930f78e

Browse files
author
Stanislav Idolov
authored
ENGCOM-1106: Correctly save Product Custom Option values #13569
2 parents 8e96f64 + 9255b42 commit 930f78e

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

app/code/Magento/Catalog/Model/Product/Option.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
1010
use Magento\Catalog\Api\Data\ProductCustomOptionValuesInterface;
11+
use Magento\Catalog\Api\Data\ProductCustomOptionValuesInterfaceFactory;
1112
use Magento\Catalog\Api\Data\ProductInterface;
1213
use Magento\Catalog\Model\Product;
1314
use 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,
@@ -391,20 +402,21 @@ public function beforeSave()
391402
*/
392403
public function afterSave()
393404
{
394-
$this->getValueInstance()->unsetValues();
395405
$values = $this->getValues() ?: $this->getData('values');
396406
if (is_array($values)) {
397407
foreach ($values as $value) {
398-
if ($value instanceof \Magento\Catalog\Api\Data\ProductCustomOptionValuesInterface) {
408+
if ($value instanceof ProductCustomOptionValuesInterface) {
399409
$data = $value->getData();
400410
} else {
401411
$data = $value;
402412
}
403-
$this->getValueInstance()->addValue($data);
404-
}
405413

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) {
408420
throw new LocalizedException(__('Select type options required values rows.'));
409421
}
410422

@@ -805,7 +817,7 @@ public function setImageSizeY($imageSizeY)
805817
}
806818

807819
/**
808-
* @param \Magento\Catalog\Api\Data\ProductCustomOptionValuesInterface[] $values
820+
* @param ProductCustomOptionValuesInterface[] $values
809821
* @return $this
810822
*/
811823
public function setValues(array $values = null)

app/code/Magento/Catalog/Model/Product/Option/Value.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class Value extends AbstractModel implements \Magento\Catalog\Api\Data\ProductCu
7878
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
7979
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
8080
* @param array $data
81+
* @param CustomOptionPriceCalculator|null $customOptionPriceCalculator
8182
*/
8283
public function __construct(
8384
\Magento\Framework\Model\Context $context,
@@ -91,6 +92,7 @@ public function __construct(
9192
$this->_valueCollectionFactory = $valueCollectionFactory;
9293
$this->customOptionPriceCalculator = $customOptionPriceCalculator
9394
?? \Magento\Framework\App\ObjectManager::getInstance()->get(CustomOptionPriceCalculator::class);
95+
9496
parent::__construct(
9597
$context,
9698
$registry,
@@ -203,19 +205,15 @@ public function getProduct()
203205
*/
204206
public function saveValues()
205207
{
208+
$option = $this->getOption();
209+
206210
foreach ($this->getValues() as $value) {
207211
$this->isDeleted(false);
208-
$this->setData(
209-
$value
210-
)->setData(
211-
'option_id',
212-
$this->getOption()->getId()
213-
)->setData(
214-
'store_id',
215-
$this->getOption()->getStoreId()
216-
);
217-
218-
if ($this->getData('is_delete') == '1') {
212+
$this->setData($value)
213+
->setData('option_id', $option->getId())
214+
->setData('store_id', $option->getStoreId());
215+
216+
if ((bool) $this->getData('is_delete') === true) {
219217
if ($this->getId()) {
220218
$this->deleteValues($this->getId());
221219
$this->delete();
@@ -224,7 +222,7 @@ public function saveValues()
224222
$this->save();
225223
}
226224
}
227-
//eof foreach()
225+
228226
return $this;
229227
}
230228

0 commit comments

Comments
 (0)