Skip to content

Commit 1d7cbf2

Browse files
author
Oleksandr Gorkun
committed
MAGETWO-73718: Bundle simple products stays unchanged after scheduled update end
1 parent 4481672 commit 1d7cbf2

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed

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-
$oldOptions = $this->optionRepository->getList($entity->getSku());
69-
if ($oldOptions) {
70-
$remainingOptions = [];
71-
$metadata
72-
= $this->metadataPool->getMetadata(ProductInterface::class);
73-
$productId = $entity->getData($metadata->getLinkField());
74-
75-
foreach ($options as $option) {
76-
$remainingOptions[] = $option->getOptionId();
77-
}
78-
foreach ($oldOptions as $option) {
79-
if (!in_array($option->getOptionId(), $remainingOptions)) {
80-
$option->setParentId($productId);
81-
$this->removeOptionLinks($entity->getSku(), $option);
82-
$this->optionRepository->delete($option);
83-
}
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)