airesvsg / acf-to-rest-api

Exposes Advanced Custom Fields Endpoints in the WordPress REST API
https://wordpress.org/plugins/acf-to-rest-api/
1.32k stars 111 forks source link

Problem with creating a new WooCommerce product with ACF fields #402

Open kamil-homer opened 2 years ago

kamil-homer commented 2 years ago

Hi I have a NodeJS app to manage my WordPress. I'm creating a new WooCommerce product with normal POST and it is working fine. Product is added.

fetch(https://my-website/wp-json/wc/v3/products, {
method: 'POST',
headers: {
   'Content-Type': 'application/json',
   Authorization: WOO_API_AUTH_REQUEST
 },
body: JSON.stringify({
   name: "product name",
   regular_price: "420",
   fields: {
      nku: "new nku"
   }
})})

image

However the "acf" values (aka 'fields') are not updated. image

The sad part is, that it works fine with regular WordPress posts, under endpoint /wp/v2/posts. Post is created and populated with ACF fields image

I was trying to use PUT on the existing product, but it doesn't update the ACF fields as well.

Options are enabled image

User in WooCommerce REST API settings has Read/Write access.

Am I missing something or is it a known issue, that we can't update ACF in the WooCommerce product?

WordPress: 6.0 ACF to REST: 3.3.3 ACF: 5.12.2 WooCommerce: Version 6.5.1

kamil-homer commented 2 years ago

I think the solution is to use meta_data instead

name: "New Product",
regular_price: "420",
fields: {
    nku: "it doesn't work",
    sku: "it doesn't work"
},
meta_data: [
     {
        "key": "nku",
        "value": "5244KA12"
     },
    {
        "key": "sku",
        "value": "123JAKIWU1"
    }
],

image image

Product is added with populated ACF fields. 'id' in the meta_data is added automatically. Looks like it needs only the key and value.

@airesvsg I let you decide if it is a feature or a bug and if the issue should be close :)