Created
August 17, 2021 03:45
-
-
Save morshedx/26b6f952a3adf7c65827764a9133a15d to your computer and use it in GitHub Desktop.
WooCommerce REST API - get product resized images url wtih width and height?
This file contains hidden or 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
/** | |
* Return all image size name in rest api | |
* And i also modified to add width and height | |
* | |
* Main idea is taking from here | |
* @link https://stackoverflow.com/a/50853300/3087033 | |
*/ | |
function prepare_product_images($response) { | |
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); | |
$response->data['images'][$key][$size] = [ | |
'src' => $image_info[0], | |
'width' => $image_info[1], | |
'height' => $image_info[2], | |
]; | |
} | |
} | |
return $response; | |
} | |
add_filter("woocommerce_rest_prepare_product_object", "prepare_product_images", 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment