19 private $productRepository;
24 private $productFactory;
29 private $configurableType;
34 private $dataObjectHelper;
39 private $optionsFactory;
44 private $attributeFactory;
57 \
Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory,
59 \
Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
64 $this->configurableType = $configurableType;
76 $product = $this->productRepository->get($sku);
82 $productTypeInstance =
$product->getTypeInstance();
87 foreach ($productTypeInstance->getUsedProducts(
$product) as $child) {
89 foreach ($child->getAttributes() as
$attribute) {
91 $value = $child->getDataUsingMethod($attrCode) ?: $child->getData($attrCode);
98 $productDataObject = $this->productFactory->create();
99 $this->dataObjectHelper->populateWithArray(
102 \
Magento\Catalog\Api\Data\ProductInterface::class
104 $childrenList[] = $productDataObject;
106 return $childrenList;
112 public function addChild($sku, $childSku)
114 $product = $this->productRepository->get($sku);
115 $child = $this->productRepository->get($childSku);
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.'));
122 $configurableProductOptions =
$product->getExtensionAttributes()->getConfigurableProductOptions();
123 if (empty($configurableProductOptions)) {
124 throw new StateException(
__(
"The parent product doesn't have configurable product options."));
128 foreach ($configurableProductOptions as $configurableProductOption) {
129 $attributeCode = $configurableProductOption->getProductAttribute()->getAttributeCode();
131 throw new StateException(
133 'The child product doesn\'t have the "%1" attribute value. Verify the value and try again.',
138 $attributeIds[] = $configurableProductOption->getAttributeId();
140 $configurableOptionData = $this->getConfigurableAttributesData($attributeIds);
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);
157 $product = $this->productRepository->get($sku);
161 __(
'The product with the "%1" SKU isn\'t a configurable product.', $sku)
168 if (
$option->getSku() == $childSku) {
173 if (count(
$options) == count($ids)) {
175 __(
"The option that was requested doesn't exist. Verify the entity and try again.")
178 $product->getExtensionAttributes()->setConfigurableProductLinks($ids);
179 $this->productRepository->save(
$product);
190 private function getOptionsFactory()
192 if (!$this->optionsFactory) {
196 return $this->optionsFactory;
205 private function getConfigurableAttributesData($attributeIds)
211 ->addFieldToFilter(
'attribute_id', $attributeIds)
217 'label' =>
$option->getLabel(),
219 'value_index' =>
$option->getValue(),
addChild($sku, $childSku)
__construct(\Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Catalog\Api\Data\ProductInterfaceFactory $productFactory, \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable $configurableType, \Magento\Framework\Api\DataObjectHelper $dataObjectHelper, \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory $attributeFactory=null)
removeChild($sku, $childSku)
$configurableAttributesData