2

I want to create a configurable product and assign some simple product to it. I have found lots of solution in the web but it's not working. Please let me know if any idea!!!

Thanks

  • via programmatically ? – zed Blackbeard Nov 15 '17 at 7:19
  • yes. via programmatically. – Haze Nov 15 '17 at 7:20
  • is setConfigurableProductsData() function work in magento 2 ? – Haze Nov 15 '17 at 7:24
3

This code has worked for me. it may be help you.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$configurable_product = $objectManager->create('\Magento\Catalog\Model\Product');

$configurable_product->setSku('CONFIG_SKU'); // set sku
$configurable_product->setName('CONFIG PRODUCT NAME'); // set name
$configurable_product->setAttributeSetId(4);
$configurable_product->setStatus(1);
$configurable_product->setTypeId('configurable');
$configurable_product->setPrice(0);
$configurable_product->setWebsiteIds(array(1)); // set website
$configurable_product->setCategoryIds(array(2)); // 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('product_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(12,13,14);

try{
$configurable_product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId); // Load Configurable Product
    $configurable_product->setAssociatedProductIds($assocIds); // Setting Associated Products
    $configurable_product->setCanSaveConfigurableAttributes(true);
    $configurable_product->save();
} catch (Exception $e) {
    echo "<pre>";
    print_r($e->getMessage());
    exit;
}
  • sorry... it is not work form me. – Haze Nov 15 '17 at 9:39
  • can you add the code ? – zed Blackbeard Nov 15 '17 at 10:23

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.