cancel
Showing results for 
Search instead for 
Did you mean: 

assign simple product to configurable product programmatically

assign simple product to configurable product programmatically

hello everyone!

i'm here again Smiley Very Happy

I've been trying at least two weeks to assign simple products to configurable products, but without success!
I tried all the existing online conditions, but nothing to do, can someone help me?

 

$associatedProductIds = array(19900,19901);
$confID = 19693;

$configurable_product = $objectManager->create('Magento\Catalog\Model\Product')->load($confID); // Load Configurable Product
$configurable_product->getTypeInstance()->setUsedProductAttributeIds(array(93),$configurable_product); //attribute ID of attribute 'size_general' in my store
$configurableAttributesData = $configurable_product->getTypeInstance()->getConfigurableAttributesAsArray($configurable_product);
$configurable_product->setCanSaveConfigurableAttributes(true);
$configurable_product->setConfigurableAttributesData($configurableAttributesData);
foreach ($associatedProductIds as $simple_product_id) {
$configurableProductsData = array();
$configurableProductsData[$simple_product_id] = array( //[$simple_product_id] = id of a simple product associated with this configurable
'0' => array(
'label' => 'Color', //attribute label
'attribute_id' => '93', //attribute ID of attribute 'size_general' in my store
'value_index' => '10', //value of 'S' index of the attribute 'size_general'
'is_percent'    => 0,
'pricing_value' => '10',
)
);
}
$configurable_product->setConfigurableProductsData($configurableProductsData);
$configurable_product->save();

I also tried to set the "super attributes" 134 and 135 in this function

$configurable_product->getTypeInstance()->setUsedProductAttributeIds(array(134,135),$configurable_product); //attribute ID of attribute 'size_general' in my store

but keep giving me error "option value are not specified"

 

'93' is attribute id of my attribute "color"

'138' is attribute id of my attriute "size"

 

thank you in advance

2 REPLIES 2

Re: assign simple product to configurable product programmatically

Hi @alessio_ottaviano

 

I understand the problem you are facing !

 

Below i am posting code for the same , kindly try with that code.

 

$product = $productFactory->create(['name'=> 'configurable product', ... ]);
$configurableOption = $optionFactory->create([]);
$linkedProduct = $linkFactory->create([]);
$product->getExtensionAttributes()->setConfigurableProductOptions($configurableOption);
$product->getExtensionAttributes()->setConfigurableProductLinks($linkedProduct);
$productRepository->save($product)

For more details refer this link - https://magento.stackexchange.com/questions/83107/programmatically-create-a-configurable-product-and...

 

As you mention you already tried all the code options from online , so you might try above one as well - but above code works for me practically so thought to shared with you 

 

Hope it helps

if issue solved,Click Kudos & Accept as Solution

Re: assign simple product to configurable product programmatically

@Manthan Davethank you for your reply!


In the code you linked, could you tell me how to define the variables in bold and underlined?

 

$product = $productFactory->create(['name'=> 'configurable product', ... ]);
$configurableOption = $optionFactory->create([]);
$linkedProduct = $linkFactory->create([]);

is part of objectmanager?

thank you