Im building an import extention, that imports bicycles from an external Database into Magento.
I managed to create working helpers, that create a configurable product and the different bikesizes as simple products.
Now I struggle to get them linked. I tried 7 "solutions" so far, none is working. The Configuration in the configurable product wont show up.
Here are my 2 helpers, that create configurable and simple product:
public function createsimpleproduct($productoptions)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product->setUrlKey($productoptions['url']);
$attid = $this->createOrGetId('marke', $productoptions['marke']);
$product->setData('marke', $attid);
$attid = $this->createOrGetId('rahmenhoehe', $productoptions['rahmenhoehe']);
$product->setData('rahmenhoehe', $attid);
$product->setSku($productoptions['sku']); // Set your sku here
$product->setName($productoptions['name']); // Name of Product
$product->setAttributeSetId(4); // 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(1); // Tax class id
$product->setTypeId('simple'); // type of product (simple/virtual/downloadable/configurable)
$product->setPrice(100); // price of product
$product->setEan($productoptions['ean']);
$product->setStockData(
array(
'use_config_manage_stock' => 1,
'manage_stock' => 1,
'is_in_stock' => 1,
'default_stock' => 10,
'qty' => 10
)
);
$product->save();
return $product;
}
And the one for the configurable product:
public function createconfigproduct($productoptions)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$product->setUrlKey($productoptions['url']);
$product->setSku($productoptions['sku']."-base"); // Set your sku here
$product->setName($productoptions['name']); // Name of Product
$product->setAttributeSetId(4); // Attribute set id
$product->setStatus(1); // Status on product enabled/ disabled 1/0
$product->setVisibility(3); // visibilty of product (catalog / search / catalog, search / Not visible individually)
$product->setTaxClassId(1); // Tax class id
$product->setTypeId('configurable'); // type of product (simple/virtual/downloadable/configurable)
$product->setPrice(100); // price of product
$product->setDescription('Geiles Teil');
$attid = $this->createOrGetId('marke', $productoptions['marke']);
$product->setData('marke', $attid);
$product->setStockData(
array(
'use_config_manage_stock' => 1,
'manage_stock' => 1,
'is_in_stock' => 1
)
);
$product->save();
return $product;
}
A third helper function, were I can just give the configurable product id and the simple product ids (as an array) into, would be really nice. But all the functions I tried so far are failing. It looks like I need the Attribute(Id), that I want to be the Attribute, I want to be the "configurable" one - That problem is already solved by 2 other functions - so no worries, if those are needed in a possible solution.
Thanks in advance
Some solutions I tried so far:
How do I create a configurable product programmatically and assign simple products?
Magento 2: How to create configurable product Programmatically
https://firebearstudio.com/blog/how-to-programmatically-create-a-configurable-magento-2-product.html
https://www.mageplaza.com/devdocs/magento-2-create-product-programmatically.html
UPDATE: As the save method from the $attributeModel is deprecated since 2.3, you can get an updated question and answer for magento > 2.3 here: Creating configurable product attribute with magento > 2.4