I've added a bunch of custom fields to my WooCommerce site - they turn up fine on the product page and I can have them returned via the API now that I've added the code below - but I can't update them via the API - expect I'm missing something really obvious :)
My code (added using PHP Inster): -
function create_ACF_meta_in_REST() {
$postypes_to_exclude = ['acf-field-group','acf-field'];
$extra_postypes_to_include = ["post"];
$post_types = array_diff(get_post_types(["_builtin" => false], 'names'),$postypes_to_exclude);
array_push($post_types, $extra_postypes_to_include);
foreach ($post_types as $post_type) {
register_rest_field( $post_type, 'ACF', [
'get_callback' => 'expose_ACF_fields',
'update_callback' => 'update_ACF_fields',
'schema' => null,
]
);
}
}
function update_ACF_fields( $data, $object ) {
$ID = $object['id'];
return update_post_meta( $ID, $data );
}
function expose_ACF_fields( $object ) {
$ID = $object['id'];
return get_fields( $ID );
}
add_action( 'rest_api_init', 'create_ACF_meta_in_REST' );
Any pointers would be much appreciated
Fixed it - easy in the end, just an undocumented feature!
For the 'standard' WooCommerce fields, you can just send JSON like this: -
{
"regular_price": "1.25"
}
That works fine and will update the field for the ID that you gave in the endpoint
BUT
For the custom fields, you have to send the following: -
{
"key": "your field name"
"value": "your new value"
}
Doing it this way works fine without any of the additional code that I showed in the question
All good in the end :)