5

I'm using WooCommerce and Advanced Custom Fields on my WordPress website. I want to add Advanced Custom Fields to WooCommerce API to get custom field value for each product.

How can I do it ?

Thanks

user1269586
  • 372
  • 1
  • 6
  • 27

1 Answers1

0

What you could do is the following

Go to Custom Fields and create a new field group - add in whatever data you need for the products and then, most importantly, in the location section ("Show this field group if") select to display the field group is equal to product from the drop down.

Then the field group will display when you have any product open.

Andrew M
  • 755
  • 9
  • 15
  • It will show custom filed for each product on my website, but I want to have also it via Woocommerce API (json) – user1269586 Mar 31 '16 at 13:43
  • Ok, I misread your question, sorry about that. From the API documentation here: http://woothemes.github.io/woocommerce-rest-api-docs/#products-properties there isn't any custom fields that can be pulled back. My only advice in this case would be to see if you could attach the data to an existing Product field – Andrew M Mar 31 '16 at 13:49
  • Ok thanks you for your answers ;-) How can I attach a custom field to an existing product field? – user1269586 Mar 31 '16 at 13:50
  • Just below the Product Data section there's a section called Custom fields and you can set them there. The ACF approach that I mentioned above is also useful. However, passing these via the API may not be possible but you can use them on the Wordpress site. – Andrew M Mar 31 '16 at 14:20
  • Yes I have already ACF on my WordPress site but it's impossible to add it to the JSON WooCommerce API ? – user1269586 Apr 04 '16 at 10:02
  • What you'll have to do in this case is to make use of one the fields in the API that you don't need. I know this is not ideal but I don't see any other option in this case – Andrew M Apr 04 '16 at 10:24
  • Ok but how can I link one of the field in the API that I don't need to the Advanced Custom Fields? – user1269586 Apr 04 '16 at 11:58
  • You cannot use ACF fields with the Woocommerce API - you can only use the fields in the API which are already part of Woocommerce. You can see a list of all those fields here http://woothemes.github.io/woocommerce-rest-api-docs/#products-properties. – Andrew M Apr 04 '16 at 13:21
  • Ok thanks you for all I will try to find a compromise ;) – user1269586 Apr 04 '16 at 15:25
  • 1
    If I write `'new_api_field' => get_field('field_key')` in function `get_product_data()` from `class-wc-api-products.php`, do you think there's a possibility like that ? Actually `'new_api_field'`return wrong value. How can I correctly access ACF values from php script ? Thanks – user1269586 Apr 07 '16 at 15:08