Home > Back-end >  Modify WooCommerce's API REST response only for specific client/key
Modify WooCommerce's API REST response only for specific client/key

Time:06-07

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;
}

CodePudding user response:

You can get the current request's user ID and their consumer key by doing something like this:

add_filter( 'woocommerce_rest_prepare_shop_order_object', 'filter_order_response', 10, 3 );
function filter_order_response( $response, $order, $request ) {
    global $wpdb;

    $consumer_key = $request->get_param( 'oauth_consumer_key' );
    $user_id = (int) $wpdb->get_var( 
        $wpdb->prepare( 
            "SELECT user_id FROM {$wpdb->prefix}woocommerce_api_keys WHERE consumer_key = %s", 
            wc_api_hash( $consumer_key ) 
        )
    );

    // Your code goes here... 

    return $response;
}

CodePudding user response:

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:

if(get_current_user_id() === <THE SPECIFIC ID>) {
  // Run your custom logic
}

Further suggestions

  1. If you're running a multi-currency shop, I would also suggest to replace the check for "CLP" with "not USD". Example:
      if ($response->data["currency"] != 'USD') {
        // Convert the amounts
      }

This would allow you to return a USD value for orders placed in any currency, not just CLP.

  1. If there's a chance that the client will fetch the same orders more than once, it could be a good idea to save the exchange rate used to calculate the USD amounts against the order meta, then use it for future calls. Exchange rates change over time, and keeping the one used for the first reply would allow your code to return consistent values.
  • Related