Skip to content

Commit 6305f08

Browse files
author
Dmytro Vilchynskyi
committed
Merge remote-tracking branch 'origin/MAGETWO-73718' into PR-13032018
2 parents 4cf59b4 + 18fe5b6 commit 6305f08

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

app/code/Magento/Bundle/Model/OptionRepository.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ public function save(
201201

202202
/** @var \Magento\Bundle\Model\Option $existingOption */
203203
$existingOption = $optionCollection->getFirstItem();
204-
if (!$optionId) {
205-
$option->setOptionId(null);
206-
}
207204
if (!$optionId || $existingOption->getParentId() != $parentId) {
205+
//If option ID is empty or existing option's parent ID is different
206+
//we'd need a new ID for the option.
207+
$option->setOptionId(null);
208208
$option->setDefaultTitle($option->getTitle());
209209
if (is_array($option->getProductLinks())) {
210210
$linksToAdd = $option->getProductLinks();

app/code/Magento/Bundle/Model/Product/SaveHandler.php

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Bundle\Model\Product;
77

8+
use Magento\Bundle\Api\Data\OptionInterface;
89
use Magento\Catalog\Api\Data\ProductInterface;
910
use Magento\Bundle\Api\ProductOptionRepositoryInterface as OptionRepository;
1011
use Magento\Bundle\Api\ProductLinkManagementInterface;
@@ -49,42 +50,58 @@ public function __construct(
4950
?: ObjectManager::getInstance()->get(MetadataPool::class);
5051
}
5152

53+
/**
54+
* @param ProductInterface $bundle
55+
* @param OptionInterface[] $currentOptions
56+
*
57+
* @return void
58+
*/
59+
private function removeOldOptions(
60+
ProductInterface $bundle,
61+
array $currentOptions
62+
) {
63+
$oldOptions = $this->optionRepository->getList($bundle->getSku());
64+
if ($oldOptions) {
65+
$remainingOptions = [];
66+
$metadata
67+
= $this->metadataPool->getMetadata(ProductInterface::class);
68+
$productId = $bundle->getData($metadata->getLinkField());
69+
70+
foreach ($currentOptions as $option) {
71+
$remainingOptions[] = $option->getOptionId();
72+
}
73+
foreach ($oldOptions as $option) {
74+
if (!in_array($option->getOptionId(), $remainingOptions)) {
75+
$option->setParentId($productId);
76+
$this->removeOptionLinks($bundle->getSku(), $option);
77+
$this->optionRepository->delete($option);
78+
}
79+
}
80+
}
81+
}
82+
5283
/**
5384
* @param object $entity
5485
* @param array $arguments
55-
* @return \Magento\Catalog\Api\Data\ProductInterface|object
86+
*
87+
* @return ProductInterface|object
88+
*
5689
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5790
*/
5891
public function execute($entity, $arguments = [])
5992
{
6093
/** @var \Magento\Bundle\Api\Data\OptionInterface[] $options */
6194
$options = $entity->getExtensionAttributes()->getBundleProductOptions() ?: [];
62-
95+
//Only processing bundle products.
6396
if ($entity->getTypeId() !== 'bundle' || empty($options)) {
6497
return $entity;
6598
}
66-
99+
/** @var ProductInterface $entity */
100+
//Removing old options
67101
if (!$entity->getCopyFromView()) {
68-
$updatedOptions = [];
69-
$oldOptions = $this->optionRepository->getList($entity->getSku());
70-
71-
$metadata = $this->metadataPool->getMetadata(ProductInterface::class);
72-
73-
$productId = $entity->getData($metadata->getLinkField());
74-
75-
foreach ($options as $option) {
76-
$updatedOptions[$option->getOptionId()][$productId] = (bool)$option->getOptionId();
77-
}
78-
79-
foreach ($oldOptions as $option) {
80-
if (!isset($updatedOptions[$option->getOptionId()][$productId])) {
81-
$option->setParentId($productId);
82-
$this->removeOptionLinks($entity->getSku(), $option);
83-
$this->optionRepository->delete($option);
84-
}
85-
}
102+
$this->removeOldOptions($entity, $options);
86103
}
87-
104+
//Saving active options.
88105
foreach ($options as $option) {
89106
$this->optionRepository->save($entity, $option);
90107
}

0 commit comments

Comments
 (0)