Reyneer Leon
Reyneer Leon

Reputation: 456

WooCommerce Rest API not updating product data using Curl

I have a WooCommerce website for developing and testing purposes and then I will code an API for a client so he can list, post, update and delete his/her products in another WooCommerce website.

My problem is that the product IS NOT being updated after using a Curl like the documentation say: https://woocommerce.github.io/woocommerce-rest-api-docs/#update-a-product

GET Requests works fine as expected and it lists the product and products:

curl -X GET https://mywebsite.com/dev/wp-json/wc/v3/products/53 \
-u ck_key:cs_key 
}

// gives this below
{
"id": 53,
"name": "iphone12",
"slug": "iphone12",
"permalink": "https:\/\/mywebsite.com\/dev\/producto\/iphone12",
"date_created": "2023-11-10T19:42:48",
"date_created_gmt": "2023-11-11T01:42:48",
"date_modified": "2023-11-11T19:49:43",
"date_modified_gmt": "2023-11-12T01:49:43",
"type": "simple",
"status": "publish",
"featured": false,
"catalog_visibility": "visible",
"description": "",
"short_description": "",
"sku": "iphone12",
"price": "2343",
"regular_price": "2343",
"sale_price": "",
"date_on_sale_from": null,
"date_on_sale_from_gmt": null,
"date_on_sale_to": null,
"date_on_sale_to_gmt": null,
"on_sale": false,
"purchasable": true,
"total_sales": 0,
"virtual": false,
"downloadable": false,
"downloads": [],
"download_limit": -1,
"download_expiry": -1,
"external_url": "",
"button_text": "",
"tax_status": "taxable",
"tax_class": "",
"manage_stock": true,
"stock_quantity": 1,
"backorders": "no",
"backorders_allowed": false,
"backordered": false,
"low_stock_amount": null,
"sold_individually": false,
"weight": "",
"dimensions": {
    "length": "",
    "width": "",
    "height": ""
},
"shipping_required": true,
"shipping_taxable": true,
"shipping_class": "",
"shipping_class_id": 0,
"reviews_allowed": true,
"average_rating": "0.00",
"rating_count": 0,
"upsell_ids": [],
"cross_sell_ids": [],
"parent_id": 0,
"purchase_note": "",
"categories": [{
    "id": 15,
    "name": "Sin categorizar",
    "slug": "sin-categorizar"
}],
"tags": [],
"images": [],
"attributes": [{
    "id": 1,
    "name": "Linea",
    "position": 0,
    "visible": true,
    "variation": true,
    "options": ["EQDA"]
}],
"default_attributes": [],
"variations": [],
"grouped_products": [],
"menu_order": 0,
"price_html": "<span class=\"woocommerce-Price-amount amount\"><bdi><span class=\"woocommerce-Price-currencySymbol\">Q<\/span>2,343.00<\/bdi><\/span>",
"related_ids": [35, 31, 32, 45, 40],
"meta_data": [],
"stock_status": "instock",
"has_options": false,
"post_password": "",
"_links": {
    "self": [{
        "href": "https:\/\/mywebsite.com\/dev\/wp-json\/wc\/v3\/products\/53"
    }],
    "collection": [{
        "href": "https:\/\/mywebsite.com\/dev\/wp-json\/wc\/v3\/products"
    }]
}
}

but then I do a Curl for updating a product like the documentation say, it does not even update it:

enter image description here

I tried this approach: https://mywebsite.com/dev/wp-json/wc/v1/products/53?name=new Iphone 15 AND IT WORKED! but.... this is not what I want because I will be using C# or any other programming language using direct Curl code that is why I need to serialize objects.

How do I solve this? Is there any way or do I have a bad configuration in my website? Did anyone had this issue before? Documentation sucks!

Upvotes: 1

Views: 431

Answers (1)

Reyneer Leon
Reyneer Leon

Reputation: 456

Done! Solved it by putting a POST instead of PUT. The documentation is wrong and maybe WooCommerce devs forgot to mention that it is a POST.

This approach worked:

curl -X POST https://mywebsite.com/dev/wp-json/wc/v3/products/53 \
-u ck_key:cs_key \
-H "Content-Type: application/text" \
-d '{
     "name": "new"
    }'

Upvotes: 2

Related Questions