使用python woocommerce API创建产品变体

时间:2018-07-18 03:44:49

标签: wordpress woocommerce

我正在尝试使用variations中的WooCommerce创建产品,但出现此错误:

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

当我从API运行create_variation函数时。

我在我创建的产品的属性上运行了GET,尽管我创建产品时打印的响应具有列出的属性,但没有找到任何属性。

这是我创建变量产品的代码:

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())

这是我创建变体的代码:

    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())

我一直在梳理头发,试图弄清楚自己在做错什么,但我现在一无所知。

感谢您的帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

这是我的变化数据,可以正常工作。

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

我发现您需要使用id,并一次更新一个版本。