'Bulk import products into Woocommerce using WP WC CLI

I'm trying to add 1000's of products into my woocommerce store using wp wc cli via my datasource which will give me the data in the following JSON format:

    {
        "name": "Product Name 1",
        "desc": "Desc 1",
        "category": "Main Category",
        "catimageName": "maincategory.jpg",
        "catimageURL": "www.dataserver.com/maincategory.jpg",
        "subcategory": [
          {
            "name": "subcat1",
            "imageName": "subcat1.jpg",
            "imageURL": "www.dataserver.com/subcat1.jpg"
          },
          {
            "name": "subcat2",
            "imageName": "subcat2.jpg",
            "imageURL": "www.dataserver.com/subcat2.jpg"
          },
          {
            "name": "subcat3",
            "imageName": "subcat3.jpg",
            "imageURL": "www.dataserver.com/subcat3.jpg"
          }
        ],
        "attributes": [
          {
            "SKU": "PP1001",
            "Height": 50,
            "Weight": 50
          }
        ],
        "prodimageName": "prod1.jpg",
        "prodimageURL": "www.dataserver.com/prod1.jpg"
      },
      {
        "name": "Product Name 2",
        "desc": "Desc 2",
        "category": "Main Category 2",
        "catimageName": "maincat2.jpg",
        "catimageURL": "www.dataserver.com/maincat2.jpg",
        "subcategory": [
          {
            "name": "subcat1",
            "imageName": "subcat1.jpg",
            "imageURL": "www.dataserver.com/subcat1.jpg"
          },
          {
            "name": "subcat2",
            "imageName": "subcat2.jpg",
            "imageURL": "www.dataserver.com/subcat2.jpg"
          },
          {
            "name": "subcat3",
            "imageName": "subcat3.jpg",
            "imageURL": "www.dataserver.com/subcat3.jpg"
          }
        ],
        "attributes": [
          {
            "SKU": "PP1002",
            "Height": 50,
            "Weight": 50
          }
        ],
        "prodimageName": "prod2.jpg",
        "prodimageURL": "www.dataserver.com/prod2.jpg"
      },
{
        "name": "Product Name 3",
        "desc": "Desc 3",
        "category": "Main Category 1",
        "catimageName": "maincat1.jpg",
        "catimageURL": "www.dataserver.com/maincat1.jpg",
        "subcategory": [
          {
            "name": "subcat1",
            "imageName": "subcat1.jpg",
            "imageURL": "www.dataserver.com/subcat1.jpg"
          },
          {
            "name": "subcat2",
            "imageName": "subcat2.jpg",
            "imageURL": "www.dataserver.com/subcat2.jpg"
          },
          {
            "name": "subcat3",
            "imageName": "subcat3.jpg",
            "imageURL": "www.dataserver.com/subcat3.jpg"
          }
        ],
        "attributes": [
          {
            "SKU": "PP1002",
            "Height": 50,
            "Weight": 50
          }
        ],
        "prodimageName": "prod3.jpg",
        "prodimageURL": "www.dataserver.com/prod3.jpg"
      },

My question is what is the best way to achieve this?

As per my research the Woocommerce REST API and wp wc cli pose too many challanges (categories , sub categories can only be used as ID's and not the actual text, so I have to insert the product categories and sub-categories, store their id trail and then link it back to the proper products).

wooCommerce Rest API - Add Product

WP WC CLI Create Product

WP WC CLI FAQ Regarding Category Text

I have also checked the Woocommerce CSV Export/Import. It works fine for my usecase but I need to do the same through my command line or programatically.

Any help in this regard is highly appreciated! TIA!

P.S - Woocommerce doesn't have a command line functionality for CSV Import

Wordpress Woocommerce Support

Github response of Woocommerce



Solution 1:[1]

Did some digging it looks like this can be done through a bash script through wp-cli via the wp wc product create command and then running it through the read -r command see the link below for more details https://remicorson.com/bulk-import-woocommerce-products-with-wp-cli-the-and-alfred/ for the .csv file it may be better to create a python script to deal with the logic/columns of the csv file through a libary to make products easier to import and then adjust the params for the product create command as needed via something like sys argv

hope this helps

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1