How can I add attributes to my product using woocommerce api in python

Viewed 810

I find difficulty on how to pass attributes to a product using the woocommerce rest api.

I successfully took the attribute id.

my function:

def assign_attribute_to_products(wcapi_yachtcharterapp,post_id,attribute_id):
    #the post_id argument is the product id
    data = {
    "attributes": [
    {
      "id": attribute_id,
    },
  ],
    }
    wcapi_yachtcharterapp.put("products/"+str(post_id), data).json()

The product is updated without passing the information of attribute_id.

Any idea how to fix this?

1 Answers

You are missing some data, take a look here

This works for me:

data = {'attributes': [{'id': 7,
            'options': ['term01', 'term02'],
            'variation': 'true',
            'visible': 'true'}]}

even though the data type for visible and variation is a boolean in the docs, you have to set 'true' or 'false' with a string. You have to include the options for variations.

Hope this helps.

Related