Woocommerce products/batch update has no response []

69 views Asked by At

I am missing something. For days I have been working on a python script to update products in woocommerce, but it fails to update and has no error or response. I have the REST API Log plugin to check whats happening and this is on of ist records:

Request Headers

{
"content_length": "1218",
"content_type": "application\/json;charset=utf-8",
"connection": "keep-alive",
"accept": "application\/json",
"accept_encoding": "gzip, deflate",
"user_agent": "WooCommerce-Python-REST-API\/3.0.0",
"host": "www.example.com",
"authorization": "Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}

Query Parameters

[]

Body Parameters

[]

Body Content

"{"update": [{"id": 8056, "manage_stock": "true", "stock_quantity": 5, "name": "Product_1", "status": "publish", "regular_price": "12.95", "categories": [{"id": 21485}]}, {"id": 44848, "manage_stock": "true", "stock_quantity": 48, "name": "Product_2", "status": "publish", "regular_price": "0.3", "categories": [{"id": 21485}]}, {"id": 48978, "manage_stock": "true", "stock_quantity": 42, "name": "Product_3", "status": "publish", "regular_price": "1.1", "categories": [{"id": 21485}]}, {"id": 8062, "manage_stock": "true", "stock_quantity": 1, "name": "Product_4", "status": "publish", "regular_price": "5.0", "categories": [{"id": 21482}]}, {"id": 21588, "manage_stock": "true", "stock_quantity": 9, "name": "Product_5", "status": "publish", "regular_price": "1.8", "categories": [{"id": 21477}]}, {"id": 49676, "manage_stock": "true", "stock_quantity": 15, "name": "T-Shirt 100% Love", "status": "publish", "regular_price": "8.9", "categories": [{"id": 21484}]}]}"

Response Headers

{
"Set-Cookie": "wfwaf-authcookie-36dbd1f8f1d69da955e78f5a9261aa9f=1%7Cadministrator%7Cmanage_options%2Cunfiltered_html%2Cedit_others_posts%2Cupload_files%2Cpublish_posts%2Cedit_posts%2Cread%7C3c79ea23ac438c0347855b3b416a54ed3518ed76b6660972e35663a7dd56841f; expires=Tue, 13-Feb-2024 204636 GMT; Max-Age=43200; path=\/; secure; HttpOnly",
"Content-Type": "application\/json; charset=UTF-8",
"X-Robots-Tag": "noindex",
"Link": "<https\/\/example.com\/wp-json\/>; rel="https\/\/api.w.org\/"",
"X-Content-Type-Options": "nosniff",
"Access-Control-Expose-Headers": "X-WP-Total, X-WP-TotalPages, Link",
"Access-Control-Allow-Headers": "Authorization, X-WP-Nonce, Content-Disposition, Content-MD5, Content-Type",
"Allow": "POST, PUT, PATCH",
"Expires": "Wed, 11 Jan 1984 050000 GMT",
"Cache-Control": "no-cache, must-revalidate, max-age=0, no-store, private"

}

Response Body

{
"data": [],
"headers": {
    "Allow": "POST, PUT, PATCH"
},
"status": 200

}

My code looks like this:

l = len(product_list)
str_start = "{\"update\": ["
str_end = "]}"
batch_size = 50
for i in range(0, l, batch_size):
    one_product_batch = product_list[i:i + batch_size]
    d = ", ".join(str(n) for n in one_product_batch)
    data = str_start + d + str_end
    print("_________________________BATCH", i, "_________________________")
    response = wcapi.post("products/batch", data).json()
    print(response)
1

There are 1 answers

0
gygoole On

So... i understand JSON a bit better now. You need to form a product like this:

product = {'id': product_id, 'manage_stock': 'true', 'stock_quantity': qty, 'name': name, 'status': 'publish', 'regular_price': price, 'categories': [{'id': category_id}]}

After appending a number of products to this list, this is how it should look before sending:


product_list = [
{
    'id': 21750, 
    'manage_stock': 'true',
    'stock_quantity': 88,
    'name': 'PRODUCT_1',
    'status': 'publish',
    'regular_price': '0.3',
    'categories': [{'id': 21480}]}, 
{
    'id': 6674,
    'manage_stock': 'true',
    'stock_quantity': 72,
    'name': 'PRODUCT_2',
    'status': 'publish',
    'regular_price': '1.1',
    'categories': [{'id': 21475}]}
]

The list will now correctly translate to JSON during sending. And this is how to send it in batches to WooCommerce:

l = len(product_list)
print("Lenght: ", l)
batch_size = 100

for i in range(0, l, batch_size):
    one_product_batch = product_list[i:i + batch_size]
    data = {'update': one_product_batch}
    # print(one_product_batch)
    print("_________________________BATCH", i, "_________________________")
    response = wcapi.post("products/batch", data).json()
    print(response)