Last active
May 2, 2018 05:35
-
-
Save kloon/de869bf9122feeb76886e4f8ea150513 to your computer and use it in GitHub Desktop.
WooCommerce API: Add different image sizes and urls to response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Do not include this line when adding the code to an existing file. | |
add_filter( 'woocommerce_rest_prepare_product_object', 'wc_api_add_image_sizes_products_response', 10, 3 ); | |
/** | |
* Add different product image sizes and URLs to the product images response. | |
* | |
* @param Object $response The response object. | |
* @param $post | |
* @param $request | |
* @return Object | |
*/ | |
function wc_api_add_image_sizes_products_response( $response, $post, $request ) { | |
global $_wp_additional_image_sizes; | |
if ( empty( $response->data ) ) { | |
return $response; | |
} | |
foreach ( $response->data['images'] as $key => $image ) { | |
foreach ( $_wp_additional_image_sizes as $size => $value ) { | |
$image_info = wp_get_attachment_image_src( $image['id'], $size ); | |
if ( is_array( $image_info ) ) { | |
$response->data['images'][ $key ]['sizes'][ $size ] = $image_info[0]; | |
} | |
} | |
} | |
return $response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment