Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit db95d8a

Browse files
author
Jeroen van Leusden
committed
Correctly save Product Custom Option values
1 parent 955fbf2 commit db95d8a

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
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,
@@ -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)

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class Value extends AbstractModel implements \Magento\Catalog\Api\Data\ProductCu
7676
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
7777
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
7878
* @param array $data
79+
* @param CustomOptionPriceCalculator|null $customOptionPriceCalculator
7980
*/
8081
public function __construct(
8182
\Magento\Framework\Model\Context $context,
@@ -89,6 +90,7 @@ public function __construct(
8990
$this->_valueCollectionFactory = $valueCollectionFactory;
9091
$this->customOptionPriceCalculator = $customOptionPriceCalculator
9192
?? \Magento\Framework\App\ObjectManager::getInstance()->get(CustomOptionPriceCalculator::class);
93+
9294
parent::__construct(
9395
$context,
9496
$registry,
@@ -201,27 +203,21 @@ public function getProduct()
201203
*/
202204
public function saveValues()
203205
{
206+
$option = $this->getOption();
207+
204208
foreach ($this->getValues() as $value) {
205-
$optionValue = clone $this;
206-
$optionValue->isDeleted(false);
207-
208-
$optionValue->setData(
209-
$value
210-
)->setData(
211-
'option_id',
212-
$optionValue->getOption()->getId()
213-
)->setData(
214-
'store_id',
215-
$optionValue->getOption()->getStoreId()
216-
);
217-
218-
if ($optionValue->getData('is_delete') == '1') {
219-
if ($optionValue->getId()) {
220-
$optionValue->deleteValues($optionValue->getId());
221-
$optionValue->delete();
209+
$this->isDeleted(false);
210+
$this->setData($value)
211+
->setData('option_id', $option->getId())
212+
->setData('store_id', $option->getStoreId());
213+
214+
if ((bool) $this->getData('is_delete') === true) {
215+
if ($this->getId()) {
216+
$this->deleteValues($this->getId());
217+
$this->delete();
222218
}
223219
} else {
224-
$optionValue->save();
220+
$this->save();
225221
}
226222
}
227223

0 commit comments

Comments
 (0)