Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LinkManagement.php
Go to the documentation of this file.
1 <?php
9 
13 
15 {
19  private $productRepository;
20 
24  private $productFactory;
25 
29  private $configurableType;
30 
34  private $dataObjectHelper;
35 
39  private $optionsFactory;
40 
44  private $attributeFactory;
45 
55  public function __construct(
56  \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
57  \Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory,
59  \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
60  \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory $attributeFactory = null
61  ) {
62  $this->productRepository = $productRepository;
63  $this->productFactory = $productFactory;
64  $this->configurableType = $configurableType;
65  $this->dataObjectHelper = $dataObjectHelper;
66  $this->attributeFactory = $attributeFactory ?: \Magento\Framework\App\ObjectManager::getInstance()
67  ->get(\Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory::class);
68  }
69 
73  public function getChildren($sku)
74  {
76  $product = $this->productRepository->get($sku);
78  return [];
79  }
80 
82  $productTypeInstance = $product->getTypeInstance();
83  $productTypeInstance->setStoreFilter($product->getStoreId(), $product);
84 
85  $childrenList = [];
87  foreach ($productTypeInstance->getUsedProducts($product) as $child) {
88  $attributes = [];
89  foreach ($child->getAttributes() as $attribute) {
90  $attrCode = $attribute->getAttributeCode();
91  $value = $child->getDataUsingMethod($attrCode) ?: $child->getData($attrCode);
92  if (null !== $value) {
93  $attributes[$attrCode] = $value;
94  }
95  }
96  $attributes['store_id'] = $child->getStoreId();
98  $productDataObject = $this->productFactory->create();
99  $this->dataObjectHelper->populateWithArray(
100  $productDataObject,
101  $attributes,
102  \Magento\Catalog\Api\Data\ProductInterface::class
103  );
104  $childrenList[] = $productDataObject;
105  }
106  return $childrenList;
107  }
108 
112  public function addChild($sku, $childSku)
113  {
114  $product = $this->productRepository->get($sku);
115  $child = $this->productRepository->get($childSku);
116 
117  $childrenIds = array_values($this->configurableType->getChildrenIds($product->getId())[0]);
118  if (in_array($child->getId(), $childrenIds)) {
119  throw new StateException(__('The product is already attached.'));
120  }
121 
122  $configurableProductOptions = $product->getExtensionAttributes()->getConfigurableProductOptions();
123  if (empty($configurableProductOptions)) {
124  throw new StateException(__("The parent product doesn't have configurable product options."));
125  }
126 
127  $attributeIds = [];
128  foreach ($configurableProductOptions as $configurableProductOption) {
129  $attributeCode = $configurableProductOption->getProductAttribute()->getAttributeCode();
130  if (!$child->getData($attributeCode)) {
131  throw new StateException(
132  __(
133  'The child product doesn\'t have the "%1" attribute value. Verify the value and try again.',
135  )
136  );
137  }
138  $attributeIds[] = $configurableProductOption->getAttributeId();
139  }
140  $configurableOptionData = $this->getConfigurableAttributesData($attributeIds);
141 
143  $optionFactory = $this->getOptionsFactory();
144  $options = $optionFactory->create($configurableOptionData);
145  $childrenIds[] = $child->getId();
146  $product->getExtensionAttributes()->setConfigurableProductOptions($options);
147  $product->getExtensionAttributes()->setConfigurableProductLinks($childrenIds);
148  $this->productRepository->save($product);
149  return true;
150  }
151 
155  public function removeChild($sku, $childSku)
156  {
157  $product = $this->productRepository->get($sku);
158 
160  throw new InputException(
161  __('The product with the "%1" SKU isn\'t a configurable product.', $sku)
162  );
163  }
164 
165  $options = $product->getTypeInstance()->getUsedProducts($product);
166  $ids = [];
167  foreach ($options as $option) {
168  if ($option->getSku() == $childSku) {
169  continue;
170  }
171  $ids[] = $option->getId();
172  }
173  if (count($options) == count($ids)) {
174  throw new NoSuchEntityException(
175  __("The option that was requested doesn't exist. Verify the entity and try again.")
176  );
177  }
178  $product->getExtensionAttributes()->setConfigurableProductLinks($ids);
179  $this->productRepository->save($product);
180  return true;
181  }
182 
190  private function getOptionsFactory()
191  {
192  if (!$this->optionsFactory) {
193  $this->optionsFactory = \Magento\Framework\App\ObjectManager::getInstance()
194  ->get(\Magento\ConfigurableProduct\Helper\Product\Options\Factory::class);
195  }
196  return $this->optionsFactory;
197  }
198 
205  private function getConfigurableAttributesData($attributeIds)
206  {
208  $attributeValues = [];
209  $attributes = $this->attributeFactory->create()
210  ->getCollection()
211  ->addFieldToFilter('attribute_id', $attributeIds)
212  ->getItems();
213  foreach ($attributes as $attribute) {
214  foreach ($attribute->getOptions() as $option) {
215  if ($option->getValue()) {
216  $attributeValues[] = [
217  'label' => $option->getLabel(),
218  'attribute_id' => $attribute->getId(),
219  'value_index' => $option->getValue(),
220  ];
221  }
222  }
224  [
225  'attribute_id' => $attribute->getId(),
226  'code' => $attribute->getAttributeCode(),
227  'label' => $attribute->getStoreLabel(),
228  'values' => $attributeValues,
229  ];
230  }
231 
233  }
234 }
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
$attributeCode
Definition: extend.phtml:12
$attributes
Definition: matrix.phtml:13
$configurableAttributesData