Title: API Woocommerce GET function &#8211; SetOrder with variable URL
Last modified: May 1, 2019

---

# API Woocommerce GET function – SetOrder with variable URL

 *  [asso99](https://wordpress.org/support/users/asso99/)
 * (@asso99)
 * [7 years ago](https://wordpress.org/support/topic/api-woocommerce-get-function-setorder-with-variable-url/)
 * 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 1 replies (of 1 total)

 *  Moderator [Jan Dembowski](https://wordpress.org/support/users/jdembowski/)
 * (@jdembowski)
 * Forum Moderator and Brute Squad
 * [7 years ago](https://wordpress.org/support/topic/api-woocommerce-get-function-setorder-with-variable-url/#post-11484988)
 * Moved to Fixing WordPress, this is not a Requests and Feedback topic.
 * Please raise plugin topics in the correct sub-forum for that plugin.
 * [https://wordpress.org/support/plugin/woocommerce/#new-post](https://wordpress.org/support/plugin/woocommerce/#new-post)

Viewing 1 replies (of 1 total)

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

## 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/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [Jan Dembowski](https://wordpress.org/support/users/jdembowski/)
 * Last activity: [7 years ago](https://wordpress.org/support/topic/api-woocommerce-get-function-setorder-with-variable-url/#post-11484988)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
