Hi all,
I am posting here in hopes that someone more experienced may be able to help me, because I’ve just spent 4 hours trying to get this to work with no luck.
I am building a headless shop using Woocommerce and a React frontend. Everything is great and I have no problems with the API in general, but I used Advanced Custom Fields to add some additional information to my products. This works and shows in the admin panel, but when requested thorugh the REST API, the `acf` field is missing from the response. The field group is enabled for REST API in ACF settings and I can actually see some of the ACF values in the `meta_data` field of the JSON response, but it’s messy and not formatted as well as the native format that ACF uses.
When used on any other post type, the `acf` field is returned correctly, along with nested objects and other cool features you’d expect.
I tried using the below snippet to manually add the ACF fields to the WooCommerce response, but I think maybe the load order is wrong, as the `get_fields()` function returns false even though there definitely are ACF fields associated with the product.
function add_acf_data_to_product_api_response($response, $post, $request) {
$product_id = $post->get_id();
$acf_data = get_fields($product_id);
if ($acf_data) {
$response->data[‘acf’] = $acf_data;
}
$response->data[‘check_if_this_works’] = $product_id;
return $response;
}
add_filter(‘woocommerce_rest_prepare_product_object’, ‘add_acf_data_to_product_api_response’, 1, 3);
The attribute `check_if_this_works` gets added correctly and has the right value of product ID, so the filter works. But for some reason the get_fields function either doesn’t exist at the point it runs, or it cannot access the post. I suspect it’s the former and that it may have something to do with the plugin loading order.
Could someone please help? I’ve literally read every post I could find about this on Google but to no avail. I don’t believe nobody has ever ran into this problem before!