vicario93
vicario93

Reputation: 33

Magento 2.2.5, how to create a configurable product with simple products associated

I want to create programmatically a configurable product with a simple product associated to it. The script is executed with no errors but I can only see the configurable product created with no assocciation, while the simple product is created but not linked with anything. Any suggest? Here it is my code:

    <?php
    use \Magento\Framework\App\Bootstrap;
    include('../app/bootstrap.php');
    $bootstrap = Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $url = \Magento\Framework\App\ObjectManager::getInstance();
    $storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface');
    $mediaurl= $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
    $state = $objectManager->get('\Magento\Framework\App\State');
    $state->setAreaCode('frontend');

    $simple_product = $objectManager->create('\Magento\Catalog\Model\Product');
    $simple_product->setSku('test-simple-32');
    $simple_product->setName('test name simple 32');
    $simple_product->setAttributeSetId(4);
    $simple_product->setColor(10);
    //$simple_product->setSize_general(10); // value id of S size
    $simple_product->setStatus(1);
    $simple_product->setTypeId('simple');
    $simple_product->setPrice(10);
    $simple_product->setWebsiteIds(array(1));
    $simple_product->setCategoryIds(array(31));
    $simple_product->setStockData(array(
        'use_config_manage_stock' => 0, //'Use config settings' checkbox
        'manage_stock' => 1, //manage stock
        'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
        'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
        'is_in_stock' => 1, //Stock Availability
        'qty' => 100 //qty
        )
    );

    $simple_product->save();

    $simple_product_id = $simple_product->getId();
    echo "simple product id: ".$simple_product_id."\n";

    //configurable product
    $configurable_product = $objectManager->create('\Magento\Catalog\Model\Product');
    $configurable_product->setSku('test-configurable-26');
    $configurable_product->setName('test name configurable 26');
    $configurable_product->setAttributeSetId(93);   
    $configurable_product->setStatus(1);
    $configurable_product->setTypeId('configurable');
    $configurable_product->setPrice(11);
    $configurable_product->setWebsiteIds(array(1));
    $configurable_product->setCategoryIds(array(31));
    $configurable_product->setStockData(array(
        'use_config_manage_stock' => 0, //'Use config settings' checkbox
        'manage_stock' => 1, //manage stock
        'is_in_stock' => 1, //Stock Availability
        )
    );

    $configurable_product->save();
    $configProductId = $configurable_product->getId();
    $attributeSetId = 93;


    $associatedProductIds=array($simple_product_id);
    $optionsFactory = $objectManager->create('\Magento\ConfigurableProduct\Helper\Product\Options\Factory');

    $configurableOptions = $optionsFactory->create($configurableProductsData);
    $extensionConfigurableAttributes = $configurable_product->getExtensionAttributes();
    $extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
    $extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
    $configurable_product->setExtensionAttributes($extensionConfigurableAttributes);    


    $associatedProductIds=array($simple_product_id);
    $configurable_product->setAssociatedProductIds($associatedProductIds);
    $configurable_product->setConfigurableProductsData($configurableProductsData); 
    $configurable_product->save();
    */


    //$associatedProductIds = array($simplProductId1,$simplProductId2,$simplProductId3,$simplProductId4);//Simple Product ids array
    $associatedProductIds = array($simple_product_id);
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($configProductId); // Load Configurable Product
    $attributeModel = $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute');
    $position = 0;
    $attributes = array(10);
    //$attributes = array($attributeColorId, $attributeSizeId); // Super Attribute Ids Used To Create Configurable Product(list of supper attribute ids what ever belong to that the attribute set under which the configurable product is)
    foreach ($attributes as $attributeId) {
        $data = array('attribute_id' => $attributeId, 'product_id' => $configProductId, 'position' => $position);
        $position++;
        $attributeModel->setData($data);//->save();
    }
    $product->setTypeId("configurable");
    $product->setAffectConfigurableProductAttributes($attributeSetId);
    $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable')->setUsedProductAttributeIds($attributes, $product);
    $product->setNewVariationsAttributeSetId($attributeSetId);
    $product->setAssociatedProductIds($associatedProductIds);// Setting Associated Products
    $product->setCanSaveConfigurableAttributes(true);
    $product->save();

    echo "configurable product id: ".$configurable_product->getId()."\n";

?>  

Upvotes: 0

Views: 691

Answers (1)

Dipesh More
Dipesh More

Reputation: 109

I will suggest you to use magmi / magmi-m2 for insertion product from outside magento .This can reduce effort of you .See the configurables product insertion here.

Upvotes: 0

Related Questions