I want to create configurable product with simple product assign.

It should be working same as like create from admin.

I have tried so many tutorials and answers. But, it's not working.

Can anyone please help me?


Try this way...


ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');

use Magento\Framework\App\Bootstrap;

 * If the external file is in the root folder
require __DIR__ . '/app/bootstrap.php';

$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager

            $category_id= array(67,68);

            //$imglist = implode(',', $insert_csv['image']);
            //echo $imagelist = array($imglist);

            $configurable_product = $objectManager->create('\Magento\Catalog\Model\Product');
            $configurable_product->setSku("test-sku"); // set sku
            $configurable_product->setName("test-name"); // set name
            $configurable_product->setWebsiteIds(array(1)); // set website
            $configurable_product->setCategoryIds($category_id); // set category
                'use_config_manage_stock' => 0, //'Use config settings' checkbox
                'manage_stock' => 1, //manage stock
                'is_in_stock' => 1, //Stock Availability

            // super attribute 
            $size_attr_id = $configurable_product->getResource()->getAttribute('size')->getId();
            $color_attr_id = $configurable_product->getResource()->getAttribute('color')->getId();

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

            $configurableAttributesData = $configurable_product->getTypeInstance()->getConfigurableAttributesAsArray($configurable_product);
            $configurableProductsData = array();
            try {
            } catch (Exception $ex) {
                echo '<pre>';

            $productId = $configurable_product->getId();

            // assign simple product ids

            $associatedProductIds = array(4609,4610,4611,4612);

            $configurable_product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId); // Load Configurable Product
                $configurable_product->setAssociatedProductIds($associatedProductIds); // Setting Associated Products
            } catch (Exception $e) {
                echo "<pre>";
  • Which type of associated product should be here? – Niket Oct 4 '19 at 6:01
  • I run this script. But, it doesn't assign simple product in configurable product. – Niket Oct 4 '19 at 6:11
  • @Niket i think you have missmetch in attribute color,size – Rakesh Donga Oct 4 '19 at 6:14
  • If i want to create that simple product with that attribute value. Then, what should i need to do? – Niket Oct 4 '19 at 6:22
  • you need to here only add your attribute code i have recently use this code and create configure products – Rakesh Donga Oct 4 '19 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.