Last active
May 21, 2018 03:33
-
-
Save kmddevdani/01d39f1f9915727353f7dd56a99f4f8d to your computer and use it in GitHub Desktop.
Magento2: Add a simple product to a configurable product
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ProductManager implements ProductManagerInterface | |
{ | |
/** | |
* @var \Magento\Catalog\Api\ProductRepositoryInterface | |
*/ | |
private $prodRep; | |
public function __construct() | |
{ | |
$this->prodRep = $this->container->get( | |
\Magento\Catalog\Api\ProductRepositoryInterface::class | |
); | |
} | |
public function prodAssign() | |
{ | |
$skuConfigurable = '11111/'; | |
$skuSimple = '11111/222/XL'; | |
// Choose two products; one simple and one configurable | |
$mySimpleProduct = $this->prodRep->get($skuSimple); | |
$myConfigurableProduct = $this->prodRep->get($skuConfigurable); | |
// The attributes "ColourCode" and "Size" must exist on the simple! | |
echo "Color: " . $mySimpleProduct->getColourCode() . PHP_EOL; | |
echo "Size: " . $mySimpleProduct->getSize() . PHP_EOL; | |
// create arrays with all available colour codes and sizes (one of each only in this example) | |
$confProd['size'] = [$mySimpleProduct->getSize()]; | |
$confProd['colour_code'] = [$mySimpleProduct->getColourCode()]; | |
// create arrays of optionValues | |
$valsProd = []; | |
$valsProd['size'] = []; | |
foreach ($confProd['size'] as $size) { | |
$myConfigurableOptionValue = $this->optionValueInterfaceFactory->create(); | |
$myConfigurableOptionValue->setValueIndex($size); | |
$valsProd['size'][] = $myConfigurableOptionValue; | |
} | |
$valsProd['colour_code'] = []; | |
foreach ($confProd['colour_code'] as $size) { | |
$myConfigurableOptionValue = $this->optionValueInterfaceFactory->create(); | |
$myConfigurableOptionValue->setValueIndex($size); | |
$valsProd['colour_code'][] = $myConfigurableOptionValue; | |
} | |
// create configurable options | |
$configurableOption1 = $this->optionInterfaceFactory->create(); | |
$configurableOption1->setAttributeId($this->getAttributeIdFromCode('colour_code')); | |
$configurableOption1->setValues($valsProd['colour_code']); | |
$configurableOption1->setLabel('Colour'); | |
$configurableOption2 = $this->optionInterfaceFactory->create(); | |
$configurableOption2->setAttributeId($this->getAttributeIdFromCode('size')); | |
$configurableOption2->setValues($valsProd['size']); | |
$configurableOption2->setLabel('Size'); | |
// Assign the configurable options to the configurable product | |
$myConfigurableProduct->getExtensionAttributes()->setConfigurableProductOptions( | |
[ | |
$configurableOption1, | |
$configurableOption2 | |
] | |
); | |
$this->prodRep->save($myConfigurableProduct); | |
// add the Simple to the Configurable | |
$this->linkManagementInterface->addChild($skuSimple, $skuConfigurable); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment