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?

asked Oct 4, 2019 at 5:47

1 Answer 1

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;
 }
answered Oct 4, 2019 at 5:58
7
  • Which type of associated product should be here? Commented Oct 4, 2019 at 6:01
  • I run this script. But, it doesn't assign simple product in configurable product. Commented Oct 4, 2019 at 6:11
  • @Niket i think you have missmetch in attribute color,size Commented Oct 4, 2019 at 6:14
  • If i want to create that simple product with that attribute value. Then, what should i need to do? Commented Oct 4, 2019 at 6:22
  • you need to here only add your attribute code i have recently use this code and create configure products Commented Oct 4, 2019 at 6:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.