Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SaveHandler.php
Go to the documentation of this file.
1 <?php
7 
15 
20 {
24  protected $optionRepository;
25 
30 
34  private $metadataPool;
35 
39  private $optionSave;
40 
47  public function __construct(
50  SaveAction $optionSave,
51  MetadataPool $metadataPool = null
52  ) {
53  $this->optionRepository = $optionRepository;
54  $this->productLinkManagement = $productLinkManagement;
55  $this->optionSave = $optionSave;
56  $this->metadataPool = $metadataPool
57  ?: ObjectManager::getInstance()->get(MetadataPool::class);
58  }
59 
68  public function execute($entity, $arguments = [])
69  {
71  $bundleProductOptions = $entity->getExtensionAttributes()->getBundleProductOptions() ?: [];
72  //Only processing bundle products.
73  if ($entity->getTypeId() !== Type::TYPE_CODE || empty($bundleProductOptions)) {
74  return $entity;
75  }
76 
77  $existingBundleProductOptions = $this->optionRepository->getList($entity->getSku());
78  $existingOptionsIds = !empty($existingBundleProductOptions)
79  ? $this->getOptionIds($existingBundleProductOptions)
80  : [];
81  $optionIds = !empty($bundleProductOptions)
82  ? $this->getOptionIds($bundleProductOptions)
83  : [];
84 
85  if (!$entity->getCopyFromView()) {
86  $this->processRemovedOptions($entity->getSku(), $existingOptionsIds, $optionIds);
87  $newOptionsIds = array_diff($optionIds, $existingOptionsIds);
88  $this->saveOptions($entity, $bundleProductOptions, $newOptionsIds);
89  } else {
90  //save only labels and not selections + product links
91  $this->saveOptions($entity, $bundleProductOptions);
92  $entity->setCopyFromView(false);
93  }
94 
95  return $entity;
96  }
97 
103  protected function removeOptionLinks($entitySku, $option)
104  {
105  $links = $option->getProductLinks();
106  if (!empty($links)) {
107  foreach ($links as $link) {
108  $this->productLinkManagement->removeChild($entitySku, $option->getId(), $link->getSku());
109  }
110  }
111  }
112 
121  private function saveOptions($entity, array $options, array $newOptionsIds = []): void
122  {
123  foreach ($options as $option) {
124  if (in_array($option->getOptionId(), $newOptionsIds, true)) {
125  $option->setOptionId(null);
126  }
127 
128  $this->optionSave->save($entity, $option);
129  }
130  }
131 
138  private function getOptionIds(array $options): array
139  {
140  $optionIds = [];
141 
142  if (!empty($options)) {
144  foreach ($options as $option) {
145  if ($option->getOptionId()) {
146  $optionIds[] = $option->getOptionId();
147  }
148  }
149  }
150 
151  return $optionIds;
152  }
153 
162  private function processRemovedOptions(string $entitySku, array $existingOptionsIds, array $optionIds): void
163  {
164  foreach (array_diff($existingOptionsIds, $optionIds) as $optionId) {
165  $option = $this->optionRepository->get($entitySku, $optionId);
166  $this->removeOptionLinks($entitySku, $option);
167  $this->optionRepository->delete($option);
168  }
169  }
170 }
__construct(OptionRepository $optionRepository, ProductLinkManagementInterface $productLinkManagement, SaveAction $optionSave, MetadataPool $metadataPool=null)
Definition: SaveHandler.php:47
$entity
Definition: element.phtml:22
$arguments