0
<?php
error_reporting(1);
set_time_limit(0);
ini_set('memory_limit', '2048M');

use Magento\Framework\App\Bootstrap;

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

/**
 * If your external file is NOT in root folder
 * Let's suppose, your file is inside a folder named 'xyz'
 *
 * And, let's suppose, your root directory path is
 * /var/www/html/magento2
 */
// $rootDirectoryPath = '/var/www/html/magento2';
// require $rootDirectoryPath . '/app/bootstrap.php';

$params = $_SERVER; 
$bootstrap = Bootstrap::create(BP, $params); 
$objectManager = $bootstrap->getObjectManager();

// Set Area Code
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML); // or \Magento\Framework\App\Area::AREA_FRONTEND, depending on your need

// Define Zend Logger
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/create-product.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);



$product = $objectManager->create('\Magento\Catalog\Model\Product');
$productRepository = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface');

// $installer = $objectManager->create(\Magento\Catalog\Setup\CategorySetup::class);
// $attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
// OR

$attributeSetId = $product->getDefaultAttributeSetId();

$attributeLabel = 'Color';
$attributeCode = 'color';
$attribute = $product->getResource()->getAttribute($attributeCode);
$options = $attribute->getOptions();
array_shift($options); //remove the first option which is empty

/**
 * Note: 
 * 
 * If the product IDs defined below in the associatedProducts array
 * are present in the database, then those products will get updated
 * 
 * If the product IDs are not present in the database
 * then new products will be added with the IDs specified
 */
$associatedProductIds = [2100, 2101];

/**
 * Create/Update Associated Simple Products
 * 
 * We have defined 2 associated products above
 * So, we will only use the first 2 options of the attribute 
 * We will link the attribute option to the product
 */
foreach ($options as $key => $option) {
    if ($key == count($associatedProductIds)) {
        break;
    }

    $productId = $associatedProductIds[$key];
    $product = $objectManager->create(\Magento\Catalog\Model\Product::class); 
    $product->setTypeId('simple')
        ->setId($productId)
        ->setAttributeSetId($attributeSetId)
        ->setWebsiteIds([1])
        ->setName('Simple Product - ' . $option->getLabel())
        ->setSku('simple_' . $productId)
        ->setPrice($productId)
        ->setColor($option->getValue()) // Set the 'color' attribute option value
        ->setVisibility(1)
        ->setStatus(1)
        ->setWeight(2)
        ->setCategories('Default')
        ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1]);

    $product = $productRepository->save($product);

    $msg = 'Created Simple Product. Product ID: ' . $product->getId();
    echo $msg . '<br />';
    $logger->info($msg);

    /** @var \Magento\CatalogInventory\Model\Stock\Item $stockItem */
     /*$stockItem = $objectManager->create(\Magento\CatalogInventory\Model\Stock\Item::class);
    $stockItem->load($productId, 'product_id');
    if (!$stockItem->getProductId()) {
        $stockItem->setProductId($productId);
    }
    $stockItem->setUseConfigManageStock(1);
    $stockItem->setQty(1000);
    $stockItem->setIsQtyDecimal(0);
    $stockItem->setIsInStock(1);
    $stockItem->save(); */

    $attributeValues[] = [
        'label' => $attributeLabel,
        'attribute_id' => $attribute->getId(),
        'value_index' => $option->getValue(),
    ];
}

$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
$optionsFactory = $objectManager->create(\Magento\ConfigurableProduct\Helper\Product\Options\Factory::class);

$configurableAttributesData = [
    [
        'attribute_id' => $attribute->getId(),
        'code' => $attribute->getAttributeCode(),
        'label' => $attribute->getStoreLabel(),
        'position' => '0',
        'values' => $attributeValues,
    ],
];
$configurableOptions = $optionsFactory->create($configurableAttributesData);

$extensionConfigurableAttributes = $product->getExtensionAttributes();
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
$extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
$product->setTypeId('configurable')
    //->setId(1)
    ->setAttributeSetId($attributeSetId)
    ->setWebsiteIds([1])
    ->setName('Test Configurable Product')
    ->setSku('test-configurable-2')
    ->setUrlKey('test-configurable-2')
    ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
    ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
    ->setStockData(['use_config_manage_stock' => 1, 'qty' => 100 ,'is_in_stock' => 1]);


$product->setExtensionAttributes($extensionConfigurableAttributes);

/**
 * Create new configurable product
 * 
 * The simple products created above
 * will get associated with the newly created configurable product.
 */

$product = $productRepository->save($product);

$msg = 'Created Configurable Product. Product ID: ' . $product->getId();
echo $msg . '<br />';
$logger->info($msg);
?>

Your Answer

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

Browse other questions tagged or ask your own question.