I want to add a key field in the products image array JSON URL: example.com/wp-json/wc/v3/products/17559 Response :
{
"id": 17559,
"name": "Nivard Trappisttenbier",
"slug": "nivard-trappisttenbier5",
"permalink": "https://example.com/home/country/austria/nivard-trappisttenbier/",
"date_created": "2022-09-14T17:20:14",
"type": "simple",
"status": "publish",
"featured": false,
"catalog_visibility": "visible",
"description": "",
"short_description": "",
"sku": "",
"price": "2.50",
"regular_price": "4.24",
"weight": "",
"dimensions": {
"length": "",
"width": "",
"height": ""
},
"categories": [
{
"id": 286,
"name": "Austria",
"slug": "austria"
}
],
"tags": [],
"images": [
{
"id": 17560,
"date_created": "2022-09-14T18:19:55",
"date_created_gmt": "2022-09-14T16:19:55",
"date_modified": "2022-09-14T18:19:55",
"date_modified_gmt": "2022-09-14T16:19:55",
"src": "https://i0.wp.com/example.com/wp-content/uploads/2022/09/nivard.png?fit=600,640&ssl=1",
"name": "nivard",
"alt": "",
"1536x1536": "https://i0.wp.com/example.com/wp-content/uploads/2022/09/nivard.png?fit=600,640&ssl=1",
"2048x2048": "https://i0.wp.com/example.com/wp-content/uploads/2022/09/nivard.png?fit=600,640&ssl=1",
"woocommerce_thumbnail": "https://example.com/wp-content/uploads/2022/09/nivard.png",
"woocommerce_single": "https://example.com/wp-content/uploads/2022/09/nivard.png",
"woocommerce_gallery_thumbnail": "https://example.com/wp-content/uploads/2022/09/nivard-100x100.png",
"ppec_logo_image_size": "https://example.com/wp-content/uploads/2022/09/nivard.png",
"ppec_header_image_size": "https://example.com/wp-content/uploads/2022/09/nivard.png"
**//"CUSTOM_KEY":"VALUE Image Link"**
}
],
"attributes": []
}
"CUSTOM_KEY":"VALUE Image Link" Please Help me to add this with Woocommerce Plugin Update independent.
i am adding this in get_images function
woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-products-controller.php
its works ideally but I want to fix this with future Woocommerce versions.
CodePudding user response:
I think you can use woocommerce_rest_prepare_product_object
filter hook here and you can modify the images data in response data if it's available.
Note: I have not tested the code, so if you run the code make sure you have backup and FTP access to fix any error if it comes up by adding this code.
/**
* Modify product rest API response data.
*
* @param WP_REST_Response $response The response object.
* @param WC_Data $object Object data.
*/
function vh_sto_wc_add_custom_data_product_api_response( $response, $product ) {
// Check if the response has data.
if ( empty( $response->data ) ) {
return $response;
}
// Check if response data has images.
// If it has then get the images.
$images = isset( $response->data['images'] ) && ! empty( $response->data['images'] ) && is_array( $response->data['images'] ) ? $response->data['images'] : array();
// If we have an image then run the next code.
if ( ! empty( $images ) ) {
// Prepare new empty array.
$new_images = array();
// Loop through images.
foreach ( $images as $image ) {
// Store old image into a new image.
$new_image = $image;
// Append new data with the key.
$new_image['YOUR_KEY_NAME'] = 'YOUR VALUE';
// Store the image into an array.
$new_images[] = $new_image;
}
// Update rest data.
$response->data['images'] = $new_images;
}
// Return response data.
return $response;
}
add_filter( 'woocommerce_rest_prepare_product_object', 'vh_sto_wc_add_custom_data_product_api_response', 10, 2 );