I have successfully created a code to modify the Woocommerce’s REST API response for order requests. It converts our store’s other currency (CLP) to USD.
The problem is that I need to use this modification only for a specific 3rd party API client/key.
I would be grateful if you could guide me or help me to achieve it.
Below are my snippets, both for WC Rest API V2/3 and Legacy.
add_filter('woocommerce_rest_prepare_shop_order_object', 'filter_order_response', 10, 3);
function filter_order_response($response, $post, $request){
if ($response->data["currency"] == 'CLP') {
$ex = 812;
$response->data["currency"] = 'USD';
$response->data["total"] = round($response->data["total"]/$ex,1);
$response->data["discount_total"] = round($response->data["discount_total"]/$ex,1);
for($i = 0; $i < count($response->data['line_items']); $i++)
{
$response->data["line_items"][$i]["total"] = round($response->data["line_items"][$i]["total"]/$ex,1);
$response->data["line_items"][$i]["subtotal"] = round($response->data["line_items"][$i]["subtotal"]/$ex,1);
$response->data["line_items"][$i]["price"] = round($response->data["line_items"][$i]["price"]/$ex,1);
}
for($i = 0; $i < count($response->data['coupon_lines']); $i++)
{
$response->data["coupon_lines"][$i]["discount"] = round($response->data["coupon_lines"][$i]["discount"]/$ex,1);
}
for($i = 0; $i < count($response->data['refunds']); $i++)
{
$response->data["refunds"][$i]["total"] = round($response->data["refunds"][$i]["total"]/$ex,1);
}
return $response;
}
else {
return $response;
}
}
Legacy WC API filter:
add_filter('woocommerce_api_order_response', 'filter_order_response_legacy', 20, 4);
function filter_order_response_legacy($order_data, $order, $fields, $server){
if ($order_data["currency"] == 'CLP') {
add_filter('woocommerce_api_customer_response', 'filter_customer_response_legacy', 10, 4);
$ex = 850;
$order_data["currency"] = 'USD';
$order_data["total"] = round($order_data["total"]/$ex,1);
$order_data["subtotal"] = round($order_data["subtotal"]/$ex,1);
$order_data["total_discount"] = round($order_data["total_discount"]/$ex,1);
for($i = 0; $i < count($order_data['line_items']); $i++)
{
$order_data["line_items"][$i]["total"] = round($order_data["line_items"][$i]["total"]/$ex,1);
$order_data["line_items"][$i]["subtotal"] = round($order_data["line_items"][$i]["subtotal"]/$ex,1);
$order_data["line_items"][$i]["price"] = round($order_data["line_items"][$i]["price"]/$ex,1);
}
for($i = 0; $i < count($order_data['coupon_lines']); $i++)
{
$order_data["coupon_lines"][$i]["amount"] = round($order_data["coupon_lines"][$i]["amount"]/$ex,1);
}
$order_data["customer"]["total_spent"] = round($order_data["customer"]["total_spent"]/$ex,1);
return $order_data;
}
else {
return $order_data;
}
}
function filter_customer_response_legacy($customer_data, $customer, $fields, $server){
$ex = 850;
$customer_data["total_spent"] = round($customer_data["total_spent"]/$ex,1);
return $customer_data;
}
2
Answers
You can get the current request’s user ID and their consumer key by doing something like this:
My recommendation would be to check the user ID under which the REST call is being handled.
WooCommerce allows you to map a set of credentials to a specific user. You can create a dedicated user, with the appropriate role and permission, take note of its ID, then check for the user ID as follows:
Further suggestions
This would allow you to return a USD value for orders placed in any currency, not just CLP.