0

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?

1

Try this way...

<?php 

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

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');
$state->setAreaCode('frontend');

$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->setAttributeSetId(4);
            $configurable_product->setStatus(1);
            $configurable_product->setTypeId('configurable');
            $configurable_product->setVisibility(4);
            $configurable_product->setPrice(0);
            $configurable_product->setWebsiteIds(array(1)); // set website
            $configurable_product->setCategoryIds($category_id); // set category
            $configurable_product->setStockData(array(
                '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);
            $configurable_product->setCanSaveConfigurableAttributes(true);
            $configurable_product->setConfigurableAttributesData($configurableAttributesData);
            $configurableProductsData = array();
            $configurable_product->setConfigurableProductsData($configurableProductsData);
            try {
                $configurable_product->save();
            } catch (Exception $ex) {
                echo '<pre>';
                print_r($ex->getMessage());
                exit;
            }

            $productId = $configurable_product->getId();



            // assign simple product ids

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

            try{
            $configurable_product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId); // Load Configurable Product
                $configurable_product->setAssociatedProductIds($associatedProductIds); // Setting Associated Products
                $configurable_product->setCanSaveConfigurableAttributes(true);
                $configurable_product->save();
            } catch (Exception $e) {
                echo "<pre>";
                print_r($e->getMessage());
                exit;
            }
  • 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.