-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Correctly save Product Custom Option values #13569
Correctly save Product Custom Option values #13569
Conversation
|
@@ -190,27 +190,29 @@ public function getProduct() | |||
public function saveValues() | |||
{ | |||
foreach ($this->getValues() as $value) { | |||
$this->isDeleted(false); | |||
$this->setData( | |||
$optionValue = clone $this; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know exactly what values need to be set on this object to save correctly? I am thinking rather than cloning here we add a factory that can create the object as needed. We should also consider the cleanup of these objects once we are done with them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That depends on what type of custom option you added I think. So no, don't think you have a predefined list of values that need to be set unless we have factories for each option type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JeroenVanLeusden do you think you could update the code to use a factory rather than a clone and see if this covers all the cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure do, hopefully have some time by the end of this week.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dmanners Code underneath makes saving stop altogether. Am I missing something?
See that a Magento\Catalog\Model\Product\Option\Value\Repository
is not present, might be an idea to add that or include the saving of custom options in Magento\Catalog\Model\Product\Option\Repository
?
foreach ($this->getValues() as $value) {
$option = $this->getOption();
$data = array_merge($value, [
'option_id' => $option->getId(),
'store_id' => $option->getStoreId()
]);
$optionValue = $this->customOptionValueFactory->create(['data' => $data]);
if ((bool) $optionValue->getData('is_delete') === true) {
if ($optionValue->getId()) {
$optionValue->deleteValues($optionValue->getId());
$optionValue->delete();
}
} else {
$optionValue->save();
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @JeroenVanLeusden I would be tempted to add the repository for just working with the option values. Though https://github.com/magento/magento2/blob/2.2-develop/app/code/Magento/Catalog/Model/Product/Option/Repository.php#L146 does deal with saving of option values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dmanners What are you proposing? Don't know what the preferred way is to get this done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @JeroenVanLeusden sorry for the delay. I would say your suggestion of adding the saving of custom options in Magento\Catalog\Model\Product\Option\Repository
would be a good option for this.
@dmanners Got it working by using a factory in |
Hi @JeroenVanLeusden let me take another look at this for you. |
Hi @JeroenVanLeusden. Thank you for your contribution. Please, consider to port this solution to 2.3 release line. |
Description
This occurred because the same value object is used for saving all values. After the save
updateStoredData()
is called and is used inprepareDataForUpdate()
.magento2/lib/internal/Magento/Framework/Model/ResourceModel/Db/AbstractDb.php
Lines 718 to 733 in 3561b66
Fixed Issues (if relevant)
Manual testing scenarios
Changes Appearance
checkboxes and save product.Contribution checklist