You could use transients:
function requestApi() {
$cache_key = 'your_transient_name';
$values = get_transient( $cache_key );
// if no data in the cache
if ( $values === false ) {
// build the URL for wp_remote_get() function
$arguments = [
'method' => 'GET',
];
$request = wp_remote_get( 'https://example.com.br/api/endpoint/product-id', $arguments );
if ( !is_wp_error( $request ) && wp_remote_retrieve_response_code( $request ) == 200 ) {
$values = json_decode( wp_remote_retrieve_body( $request ) );
// print_r( $values ); // use it to see all the returned values!
set_transient( $cache_key, $values, 7200 ); // 2 hours cache
} else {
return; // you can use print_r( $values ); here for debugging
}
}
return $values;
}
With this solution, the remote query is cached via the transients, which are stored in the wp_options, and re-requested at arbitrary time intervals.
This saves you valuable resources.
The principle is described here a little more in detail: