Title: API Woocommerce GET function – SetOrder with variable URL
Last modified: May 2, 2019

---

# API Woocommerce GET function – SetOrder with variable URL

 *  Resolved [asso99](https://wordpress.org/support/users/asso99/)
 * (@asso99)
 * [7 years ago](https://wordpress.org/support/topic/api-woocommerce-get-function-setorder-with-variable-url-2/)
 * First of all I would like to say that I’m new to the programming world and that
   I will appreciate your help with my following question.
    Basically I would like
   to add an API call as soon as an order is completed and being actually paid. 
   So I found this function online.
 *     ```
       add_action( 'woocommerce_payment_complete', 'my_api_call');
       function my_api_call( $order_id ){
   
       	// Order Setup Via WooCommerce
   
       	$order = new WC_Order( $order_id );
   
       	// Iterate Through Items
   
       	$items = $order->get_items(); 
       	foreach ( $items as $item ) {	
   
       		// Store Product ID
   
       	$product_id = $item['product_id'];
               $product = new WC_Product($item['product_id']);
   
               // Check for "API" Category and Run
   
               if ( has_term( 'api', 'product_cat', $product_id ) ) {
   
       	       	$name		= $order->billing_first_name;
               	$surname	= $order->billing_last_name;
               	$email		= $order->billing_email;
       	        $projectsku     = $product->get_sku(); 
               	$apikey 	= "KEY_GOES_HERE";
   
               	// API Callout to URL
   
               	$url = '##API URL##';
   
       			$body = array(
       				"Project"	=> $projectsku,
       				"Name" 		=> $name,
       				"Surname"  	=> $surname,
       				"Email"		=> $email,
       				"KEY"		=> $apikey
       			);
   
       			$response = wp_remote_post( $url, 
       				array(
       					'headers'   => array('Content-Type' => 'application/json; charset=utf-8'),
       					'method'    => 'POST',
       					'timeout' => 75,				    
       					'body'		=> json_encode($body),
       				)
       			);
   
       			$vars = json_decode($response['body'],true);
   
                               // API Response Stored as Post Meta
   
         			update_post_meta( $order_id, 'meta_message_'.$projectsku, $vars['message'] );
         			update_post_meta( $order_id, 'meta_link_'.$projectsku, $vars['link']);
         			update_post_meta( $order_id, 'did-this-run','yes'); // just there as a checker variable for me
               }
   
           }
       }
       ```
   
 * In this function I would like to have a variable URL as the example below:
 * > [http://server_name/application_name/Myapi/Productslist/setOrder?DBContext=Default&purchase_no=2&order_no=2&barcode=5009736400011&ordQty=1&sellPrice=300.22&key=password](http://server_name/application_name/Myapi/Productslist/setOrder?DBContext=Default&purchase_no=2&order_no=2&barcode=5009736400011&ordQty=1&sellPrice=300.22&key=password)
 * Which is a GET function in which the variables are:
    purchase_no order_no barcode
   ordQty sellPrice
 * And “key” needs to be a fixed string.
 * So, do I need to add all the variables under array?
 *     ```
       $url = '##API URL##';
   
       			$body = array(
       				"Project"	=> $projectsku,
       				"Name" 		=> $name,
       				"Surname"  	=> $surname,
       				"Email"		=> $email,
       				"KEY"		=> $apikey
       			);
       ```
   
 * Moreover do I need to change the method to GET in order for it to work?
 *     ```
       $response = wp_remote_post( $url, 
       				array(
       					'headers'   => array('Content-Type' => 'application/json; charset=utf-8'),
       					'method'    => 'POST',
       					'timeout' => 75,				    
       					'body'		=> json_encode($body),
       				)
       			);
       ```
   
 * Thank you very much in advance!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [madeincosmos](https://wordpress.org/support/users/madeincosmos/)
 * (@madeincosmos)
 * Automattic Happiness Engineer
 * [6 years, 11 months ago](https://wordpress.org/support/topic/api-woocommerce-get-function-setorder-with-variable-url-2/#post-11568503)
 * Hi [@asso99](https://wordpress.org/support/users/asso99/),
 * If you’d like WooCommerce to send an API call to a server at a different address
   whenever someone pays for an order, this can be achieved without custom coding
   by using WooCommerce Webhooks. If you set up a webhook, it will automatically
   send a notification to the given address. There’s also an option to add a `Secret`
   in webhook settings that serves as an authentication key:
 * [https://docs.woocommerce.com/document/webhooks/](https://docs.woocommerce.com/document/webhooks/)
 * If you need to additionally modify the API message response sent by the webhook(
   i.e. rename certain columns), you can use the filter `woocommerce_api_order_response`
   to change the response before it is sent. Here I found an example of how this
   can be done:
 * [http://dominykasgel.com/modify-woocommerce-api-orders-response/](http://dominykasgel.com/modify-woocommerce-api-orders-response/)
 * Cheers!
 *  [madeincosmos](https://wordpress.org/support/users/madeincosmos/)
 * (@madeincosmos)
 * Automattic Happiness Engineer
 * [6 years, 11 months ago](https://wordpress.org/support/topic/api-woocommerce-get-function-setorder-with-variable-url-2/#post-11599978)
 * We haven’t heard back from you in a while, so I’ll mark this thread as resolved
   now. If you have some more questions, feel free to start a new one.
 * Cheers!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘API Woocommerce GET function – SetOrder with variable URL’ is closed 
to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

 * [api](https://wordpress.org/support/topic-tag/api/)
 * [get](https://wordpress.org/support/topic-tag/get/)
 * [get function](https://wordpress.org/support/topic-tag/get-function/)

 * 2 replies
 * 2 participants
 * Last reply from: [madeincosmos](https://wordpress.org/support/users/madeincosmos/)
 * Last activity: [6 years, 11 months ago](https://wordpress.org/support/topic/api-woocommerce-get-function-setorder-with-variable-url-2/#post-11599978)
 * Status: resolved