Magento2: Associating a simple product with a configurable product

Created on 27 Jun 2016  路  20Comments  路  Source: magento/magento2

What is the correct way to associate an existing simple product with an existing configurable product?

I've been struggling with this for a couple of weeks now. I _somehow_ got it to work im 2.0 using the API classes in Magento\ConfigurableProduct\Api, but that's broken in 2.1. The added products don't show up in the configurable's admin page.

I found some blog posts and Stack Overflow questions related to this, but they either don't work or they don't use the API interfaces.

Format is not valid needs update improvement

Most helpful comment

@develpr This can't be the only way right now, can it? If it is, then that's a _massive_ oversight.

Magento, what's the plan here? Is this even planned? @benmarks maybe?

All 20 comments

Hi @MidnightDesign , please provide detailed steps to reproduce the issue.

Thanks,
Anton.

I guess this is more of a documentation issue/question. How is this _supposed_ to work? The code that worked for me in 2.0 is very convoluted and so it's not easy to break it down to a simple reproducable thing here.

Basically, I'm using a combination of Magento\ConfigurableProduct\Api\OptionRepositoryInterface and Magento\ConfigurableProduct\Api\LinkManagementInterface#addChild(). Is this even the right direction? Or are those interfaces meant for something different?

@piatrouski Yes, your issue might be connected to this. But here I'm asking in general how configurable products are supposed to work.

@MidnightDesign in 1.x you could create a product, then associated simple products to the configurable product by using the standard filter grid to filter / find simple products to associated. With M2 you need to first use the "generation" tool to create the simple products to associate, then you need to replace the auto-generated versions with the simple products you actually want. As I see it there is a completely unneeded first step of creating the autogenerated products.

I would say that you should be able to select simple products to relate to the configurable product _without_ having to first generate the simple products.

@develpr This can't be the only way right now, can it? If it is, then that's a _massive_ oversight.

Magento, what's the plan here? Is this even planned? @benmarks maybe?

Hi @MidnightDesign , we have internal ticket MAGETWO-50639 in our backlog to revise the flow for the configurable product.

As for now, the flow described by @develpr is correct. "Add products manually" isn't available until user defines attributes within "Create/Edit Configurations" wizard.

https://github.com/magento/magento2/issues/2564

Best,
Anton.

@antboiko Thanks for the clarification, but just to be sure: you know I'm talking about doing this in code, not in the admin backend, right? I can't imagine there's no way to do this programmatically. (I've done it in 2.0 - the 2.1 update just broke my hacky code.)

Sorry @MidnightDesign I may have led the discussion on a wild goose chase / off course! I was referring to the admin interface, not code / API.

@MidnightDesign
Did you check the solution from the previous comment?

Not yet. That's very convoluted and it would take some effort to pick out the parts that I actually need and adapt them to my use case.

Thank you for your submission.

We recently made some changes to the way we process GitHub submissions to more quickly identify and respond to core code issues.

Feature Requests and Improvements should now be submitted to the new Magento 2 Feature Requests and Improvements forum (see details here).

We are closing this GitHub ticket and have moved your request to the new forum.

@antboiko we tested this and it works only during configurable product creation. So if you have simples created before and create new configurable product. Simples that was created before will be assigned to configurable.

If you have configurable already created and create new simple and want to assign to existing configurable it doesn't work.

I am also facing the same problem.Any one get solution for this issue in Magento 2.1??

I am finding that I am not able to add a new simple product to a previously set up configurable product. Am I missing something?
v2.1.2

I am trying to create configrable product programatically. But having problem in the 2.1> version as simple products are not assigned to configrable products but in version 2.0.7 it's working fine. What is the issue?
My code is as below.

`$objectManager = MagentoFrameworkAppObjectManager::getInstance(); // instance of object manager
$configurable_product = $objectManager->create('MagentoCatalogModelProduct');
$configurable_product->setSku($upc); // Set your sku here
$configurable_product->setName($upc); // Name of Product
//$configurable_product->setDescription($productDescription);
$configurable_product->setAttributeSetId(16);
$configurable_product->setStatus(1);
$configurable_product->setTypeId('configurable');
$configurable_product->setPrice($salesPrice);
$configurable_product->setWebsiteIds(array(1));
//$configurable_product->setCategoryIds(array(31));
$configurable_product->setStockData(array(
'use_config_manage_stock' => 0, //'Use config settings' checkbox
'manage_stock' => 1, //manage stock
'is_in_stock' => 1, //Stock Availability
)
);

$configurable_product->save();
echo "configurable product id: ".$configurable_product->getId()."


";
//Set Config product and ID
$_configProduct[$product_data->upc] = $configurable_product->getId();
//$config_id = $configurable_product->getId();

//Creat simple product
$objectManager = MagentoFrameworkAppObjectManager::getInstance(); // instance of object manager
$product = $objectManager->create('MagentoCatalogModelProduct');
$product->setSku($sku); // Set your sku here
$product->setName($productName); // Name of Product
$product->setDescription($productDescription);
$product->setAttributeSetId(16); // Attribute set id
$product->setStatus(1); // Status on product enabled/ disabled 1/0
$product->setWeight(10); // weight of product
$product->setVisibility(4); // visibilty of product (catalog / search / catalog, search / Not visible individually)
$product->setTaxClassId(0); // Tax class id
$product->setTypeId('simple'); // type of product (simple/virtual/downloadable/configurable)
$product->setPrice($salesPrice); // price of product
$product->setWebsiteIds(array(1));
$product->setStockData(
array(
'use_config_manage_stock' => 0,
'manage_stock' => 1,
'is_in_stock' => 1,
'qty' => $stockQty
)
);

//For Attribute Options
foreach($tags as $tag) {
$name = strtolower($tag->name);
$value = $tag->value;

$eavConfig = $objectManager->get('MagentoEavModelConfig');
$attribute = $eavConfig->getAttribute('catalog_product', $name);
$options = $attribute->getSource()->getAllOptions();

$optionsExists = array();
foreach($options as $option) {
$optionsExists[] = $option['label'];
}
//Check if options are available in E-commerce
if(in_array($value, $optionsExists)) {
$key = array_search($value, array_column($options, 'label'));
if($name == 'color'){
$product->setColor($options[$key]['value']);
}
if($name == 'size'){
$product->setSize($options[$key]['value']);
}
} else {
//If options are not available then create options programatically
$option = array();
$newAttributeValue = $value;
$attr = $objectManager->create('MagentoEavModelEntityAttribute');
$saveAttribute = $attr->loadByCode('catalog_product',$name);
$option['value'][$newAttributeValue][0] = $newAttributeValue ;
$saveAttribute->addData(array('option' => $option));
$saveAttribute->save();

//Custom Query to get Attribute option data
$resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
$connection = $resource->getConnection();

$sql = 'SELECT option.attribute_id, option.option_id as value,value.value as label
FROM eav_attribute_option AS option
INNER JOIN eav_attribute AS attribute
ON attribute.attribute_id = option.attribute_id
INNER JOIN eav_attribute_option_value AS value
ON value.option_id = option.option_id
WHERE attribute.attribute_code="'.$name.'"';
$attributeCollection = $connection->fetchAll($sql);
$key = array_search($value, array_column($attributeCollection, 'label'));
if($name == 'color'){
$product->setColor($attributeCollection[$key]['value']);
}
if($name == 'size'){
$product->setSize($attributeCollection[$key]['value']);
}
}
}
$product->save();
$proudct_id = $product->getId();
echo "SImple Product Id".$proudct_id."----------------------------------


";

//Assign simple product to configrable product
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$assignProduct = $objectManager->create('MagentoCatalogModelProduct')->load($configurable_product->getId()); // Load Configurable Product
$attributeModel = $objectManager->create('MagentoConfigurableProductModelProductTypeConfigurableAttribute');
$position = 0;
$attributes = array(90, 137); // Super Attribute Ids Used To Create Configurable Product(color id,size id)
$associatedProductIds[] = $proudct_id; //Product Ids Of Associated Products

foreach ($attributes as $attributeId) {
$data = array('attribute_id' => $attributeId, 'product_id' => $configurable_product->getId(), 'position' => $position);
$position++;
$attributeModel->setData($data)->save();
}
//$objectManager->create('MagentoConfigurableProductModelProductTypeConfigurable')->setUsedProductAttributeIds($attributes, $assignProduct);
$assignProduct->setAssociatedProductIds($associatedProductIds);// Setting Associated Products
$assignProduct->setCanSaveConfigurableAttributes(true);
$assignProduct->save();`

@antboiko The link provided goes to a 404.

@MidnightDesign Have you had any luck getting this to work?

Hey, you can always try a third-party app like mag-manager.com here is up to date article about this issue
https://www.mag-manager.com/useful-articles/tipstricks/massively-associate-child-simple-products-to-main-configurable-via-import-to-magento/

Was this page helpful?
0 / 5 - 0 ratings