Skip to content

Instantly share code, notes, and snippets.

@scottopolis
Last active June 20, 2022 14:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottopolis/1fa6400ddf0be63b40d3caa9985d3646 to your computer and use it in GitHub Desktop.
Save scottopolis/1fa6400ddf0be63b40d3caa9985d3646 to your computer and use it in GitHub Desktop.
Modify WooCommerce REST API Product Response
<?php
// add this code to a custom plugin
add_filter( 'woocommerce_rest_prepare_product_object', 'wc_app_add_custom_data_to_product', 10, 3 );
// filter the product response here
function wc_app_add_custom_data_to_product( $response, $post, $request ) {
// in this case we want to display the short description, so we copy it over to the description, which shows up in the app
$response->data['description'] = $response->data['short_description'];
return $response;
}
@ahmedelatab
Copy link

Finally I had found the correct way for modifying the WC product REST API response.

I were missing "_object" after "woocommerce_rest_prepare_product" in the name of the filter.

Thanks!

@dans-art
Copy link

Thanks! Works great with Woocommerce 4.7.0 on WP 5.5.3

@naumanahmed19
Copy link

Does it still work? I have added in my function.php but not getting any changes.

@matthewfarlymn
Copy link

@naumanahmed19 it looks like the correct filter now includes object woocommerce_rest_prepare_product_object http://hookr.io/actions/woocommerce_rest_prepare_product_object/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment