BMitrano825
BMitrano825

Reputation: 123

Create product variations with python woocommerce api

I am attempting to create a product with variations in WooCommerce but I am getting this error:

{u'message': u'No route was found matching the URL and request method', u'code': u'rest_no_route', u'data': {u'status': 404}}

when I run the create_variation function from the API.

I ran a GET on the attributes for the product I created and it found no attributes even though the printed response when I created the product had the attributes listed.

Here is my code to create the variable product:

data = {

    "name": row[3],
    "type": "variable",
    "description": row[4],
    "images": [
        {
            "src": row[15],
            "position": 0
        }
    ],
    "in_stock": True,
    "sku": row[2],
    'attributes': [
        {
            'name': 'Size',
            'variation': True,
            'visible': True,
            'options': sizeList,
        },
        {
            'name': 'Color',
            'variation': True,
            'visible': True,
            'options': colorList,
        }
    ],
}


print(wcapiNew.post("products", data).json())

Here is my code to create the variations:

    result = wcapi.get("products/sku/"+row[2]).json()
    product_id = result['product']['id']

    variationData = {
        "regular_price": row[17],
        "image": {
            "src": row[13]
        },
        "sku": row[19],
        "attributes": [
            {
                "name": "Color",
                "option": row[6]
            },
            {
                "name": "Size",
                "option": row[10]
            }
        ]
    }

    print(wcapiNew.post("products/"+str(product_id)+"/variations", variationData).json())

I've been tearing my hair out trying to figure out what I'm doing wrong but I'm clueless right now.

Any help is appreciated. Thanks.

Upvotes: 2

Views: 1460

Answers (1)

This is my variations data, and it work.

data_1 = {
    "regular_price": "9.00",
    "sku": "premium-quality-101-red",
    "attributes": [
    {
        "id": 1,
        "option": "Red"
    }]
}

I figure out that you need to use id, and update one variation at a time.

Upvotes: 1

Related Questions