How to import configurable products programmatically in Magento 2

Here, We will explain How to import configurable products programmatically in Magento 2.

First off, we will make it as an ‘External php file’ so we can use it for testing purposes. Ultimately you would need to do a command line with it so its fully integrated. However if you know how to create your own Magento 2 module it should come easy for you.

We will use the latest Magento 2 version which is 2.1.7 at the time of this writing. The reason is that I have found it to be the most ‘stable’ version so far.

So here we go, as you know you must create the simple product first so you can associate it to your configurable product that is ‘ALREADY CREATED’. Already created is in all caps here. Because there is not much on google that tells you how to successfully do it !

1- CREATE THE PHP FILE

create a file named : createsimples.php per example which you will connect to a csv file or an external data source

2- CREATE THE NEW PRODUCT PROGRAMATICALLY

after you loaded your data source to get your products from in your foreach (or wherever you decide to put it) place this code :

Right after place the following code. Note that your variable needs to be declared for each ‘setX()’

Now if you want to add an image to your product :

3- CREATE THE CONFIGURABLE PRODUCT FROM THE SIMPLE PRODUCTS

After you verified that your simple products were created with success, now comes the task of creating the configurable products from them.

your configurable product should be updated with the new formats / attribute options that you want to include in it. Remember to test and BACKUP as it may not always work the first time.