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  private $optionRepository;
25 
29  private $resourceModel;
30 
37  public function __construct(
38  ResourceModelConfigurable $resourceModel,
39  OptionRepositoryInterface $optionRepository
40  ) {
41  $this->resourceModel = $resourceModel;
42  $this->optionRepository = $optionRepository;
43  }
44 
51  public function execute($entity, $arguments = [])
52  {
53  if ($entity->getTypeId() !== Configurable::TYPE_CODE) {
54  return $entity;
55  }
56 
57  $extensionAttributes = $entity->getExtensionAttributes();
58  if ($extensionAttributes === null) {
59  return $entity;
60  }
61 
62  if ($extensionAttributes->getConfigurableProductOptions() !== null) {
63  $this->deleteConfigurableProductAttributes($entity);
64  }
65 
66  $configurableOptions = (array) $extensionAttributes->getConfigurableProductOptions();
67  if (!empty($configurableOptions)) {
68  $this->saveConfigurableProductAttributes($entity, $configurableOptions);
69  }
70 
71  $configurableLinks = $extensionAttributes->getConfigurableProductLinks();
72  if ($configurableLinks !== null) {
73  $configurableLinks = (array)$configurableLinks;
74  $this->resourceModel->saveProducts($entity, $configurableLinks);
75  }
76 
77  return $entity;
78  }
79 
87  private function saveConfigurableProductAttributes(ProductInterface $product, array $attributes): array
88  {
89  $ids = [];
90  $existingAttributeIds = [];
91  foreach ($this->optionRepository->getList($product->getSku()) as $option) {
92  $existingAttributeIds[$option->getAttributeId()] = $option;
93  }
95  foreach ($attributes as $attribute) {
96  if (!in_array($attribute->getAttributeId(), array_keys($existingAttributeIds))
97  || $this->isOptionChanged($existingAttributeIds[$attribute->getAttributeId()], $attribute)
98  ) {
99  $attribute->setId(null);
100  $ids[] = $this->optionRepository->save($product->getSku(), $attribute);
101  }
102  }
103 
104  return $ids;
105  }
106 
113  private function deleteConfigurableProductAttributes(ProductInterface $product): void
114  {
115  $newAttributeIds = [];
116  foreach ($product->getExtensionAttributes()->getConfigurableProductOptions() as $option) {
117  $newAttributeIds[$option->getAttributeId()] = $option;
118  }
119  foreach ($this->optionRepository->getList($product->getSku()) as $option) {
120  if (!in_array($option->getAttributeId(), array_keys($newAttributeIds))
121  || $this->isOptionChanged($option, $newAttributeIds[$option->getAttributeId()])
122  ) {
123  $this->optionRepository->deleteById($product->getSku(), $option->getId());
124  }
125  }
126  }
127 
135  private function isOptionChanged(OptionInterface $option, Attribute $attribute): bool
136  {
137  if ($option->getLabel() == $attribute->getLabel() && $option->getPosition() == $attribute->getPosition()) {
138  return false;
139  }
140 
141  return true;
142  }
143 }
__construct(ResourceModelConfigurable $resourceModel, OptionRepositoryInterface $optionRepository)
Definition: SaveHandler.php:37
$resourceModel
Definition: tablerates.php:10
$entity
Definition: element.phtml:22
$attributes
Definition: matrix.phtml:13
$arguments