Skip to content

Instantly share code, notes, and snippets.

@qant
Last active April 26, 2023 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qant/fb3077c877e6147dd398f4631e988e4e to your computer and use it in GitHub Desktop.
Save qant/fb3077c877e6147dd398f4631e988e4e to your computer and use it in GitHub Desktop.
modify wordpress rest request
<?php
/**
* @see \WP_REST_Server::respond_to_request();
*
* @param \WP_REST_Response|\WP_HTTP_Response|\WP_Error|mixed $response Result to send to the client.
* Usually a WP_REST_Response or WP_Error.
* @param array $handler Route handler used for the request.
* @param \WP_REST_Request $request Request used to generate the response.
*/
function modify_rest_request( $response, array $handler, \WP_REST_Request $request ) {
// Check are we on the products route.
if ( 1 !== preg_match( '#/wc/v3/products/(?P<id>[\d]+)#', $request->get_route() ) ) {
return $response;
}
// TODO: Check $_SERVER or $_REQUEST headers to see is it printify making the request.
// TODO: Create an hourly cron job to update the exchange rate.
$exchange_rate = 1.35;
if ( $request->has_param( 'regular_price' ) ) {
$new_price_usd = $request->get_param( 'regular_price' );
$new_price_cad = $exchange_rate * $new_price_usd;
$request->set_param( 'regular_price', $new_price_cad );
}
return $response;
}
add_filter( 'rest_request_before_callbacks', 'modify_rest_request', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment