|
5 | 5 | */
|
6 | 6 | namespace Magento\Bundle\Model\Product;
|
7 | 7 |
|
| 8 | +use Magento\Bundle\Api\Data\OptionInterface; |
8 | 9 | use Magento\Catalog\Api\Data\ProductInterface;
|
9 | 10 | use Magento\Bundle\Api\ProductOptionRepositoryInterface as OptionRepository;
|
10 | 11 | use Magento\Bundle\Api\ProductLinkManagementInterface;
|
@@ -49,42 +50,58 @@ public function __construct(
|
49 | 50 | ?: ObjectManager::getInstance()->get(MetadataPool::class);
|
50 | 51 | }
|
51 | 52 |
|
| 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 | + |
52 | 83 | /**
|
53 | 84 | * @param object $entity
|
54 | 85 | * @param array $arguments
|
55 |
| - * @return \Magento\Catalog\Api\Data\ProductInterface|object |
| 86 | + * |
| 87 | + * @return ProductInterface|object |
| 88 | + * |
56 | 89 | * @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
57 | 90 | */
|
58 | 91 | public function execute($entity, $arguments = [])
|
59 | 92 | {
|
60 | 93 | /** @var \Magento\Bundle\Api\Data\OptionInterface[] $options */
|
61 | 94 | $options = $entity->getExtensionAttributes()->getBundleProductOptions() ?: [];
|
62 |
| - |
| 95 | + //Only processing bundle products. |
63 | 96 | if ($entity->getTypeId() !== 'bundle' || empty($options)) {
|
64 | 97 | return $entity;
|
65 | 98 | }
|
66 |
| - |
| 99 | + /** @var ProductInterface $entity */ |
| 100 | + //Removing old options |
67 | 101 | 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); |
86 | 103 | }
|
87 |
| - |
| 104 | + //Saving active options. |
88 | 105 | foreach ($options as $option) {
|
89 | 106 | $this->optionRepository->save($entity, $option);
|
90 | 107 | }
|
|
0 commit comments