Skip to content
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

Merged
merged 3 commits into from Jul 14, 2018

Conversation

JeroenVanLeusden
Copy link
Member

@JeroenVanLeusden JeroenVanLeusden commented Feb 8, 2018

Description

This occurred because the same value object is used for saving all values. After the save updateStoredData() is called and is used in prepareDataForUpdate().

protected function prepareDataForUpdate($object)
{
$data = $object->getData();
foreach ($object->getStoredData() as $key => $value) {
if (array_key_exists($key, $data) && $data[$key] === $value) {
unset($data[$key]);
}
}
$dataObject = clone $object;
$dataObject->setData($data);
$data = $this->_prepareDataForTable($dataObject, $this->getMainTable());
unset($data[$this->getIdFieldName()]);
unset($dataObject);
return $data;
}

Fixed Issues (if relevant)

  1. Custom option values do not save correctly #5067: Custom option values do not save correctly

Manual testing scenarios

  1. Install module ReachDigital_CustomOption.zip
  2. An extra column as added to the Product Custom Options (http://cloud.h-o.nl/pP3N).
  3. Uncheck all Changes Appearance checkboxes and save product.
  4. Only the first checkbox is saved.

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

@magento-cicd2
Copy link
Contributor

magento-cicd2 commented Feb 8, 2018

CLA assistant check
Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.

@magento-engcom-team magento-engcom-team added this to the March 2018 milestone Mar 27, 2018
@magento-engcom-team magento-engcom-team added partners-contribution Pull Request is created by Magento Partner Partner: Reach Digital Pull Request is created by partner Reach Digital labels Mar 29, 2018
@@ -190,27 +190,29 @@ public function getProduct()
public function saveValues()
{
foreach ($this->getValues() as $value) {
$this->isDeleted(false);
$this->setData(
$optionValue = clone $this;
Copy link
Contributor

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.

Copy link
Member Author

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?

Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Member Author

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();
    }
}

Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

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.

@okorshenko okorshenko modified the milestones: March 2018, April 2018 Apr 16, 2018
@JeroenVanLeusden
Copy link
Member Author

@dmanners Got it working by using a factory in Magento\Catalog\Model\Product\Option::afterSave(). Can you share your thoughts?

@dmanners
Copy link
Contributor

Hi @JeroenVanLeusden let me take another look at this for you.

@magento-engcom-team
Copy link
Contributor

Hi @JeroenVanLeusden. Thank you for your contribution.
We will aim to release these changes as part of 2.2.6.
Please check the release notes for final confirmation.

Please, consider to port this solution to 2.3 release line.
You may use Porting tool to port commits automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants