Is there a way to extend the products-object in the woocommerce rest api /wp-json/wc/v3/products
, in a non-destructive way, so plugins that uses that endpoint, doesn’t break.
I’ve currently tried create my own rest endpoint to replicate the object, but that is now missing alot of data ofc.
I’ve also tried something like this, but it didn’t seem to work.
add_filter( 'woocommerce_rest_prepare_product', 'wc_rest_api_add_custom_data_to_product', 90, 2 );
function wc_rest_api_add_custom_data_to_product( $response, $post ) {
// retrieve a custom field and add it to API response
$response->data['your_new_key'] = get_post_meta( $post->ID, '_your_custom_filed_key', true );
return $response;
}
Source: https://francescocarlucci.com/woocommerce/woocommerce-api-custom-data-default-endpoints/
Any help in this regard would be much appreciated.
2
Answers
I found a way of extending the endpoint with register_rest_field.
That way I can both extend (add) and overwrite (update) an object.
Source: https://developer.wordpress.org/reference/functions/register_rest_field/
Here a way to change prices. Maybe you can use the same process?
Source: Change product prices via a hook in WooCommerce 3