• Resolved asso99

    (@asso99)


    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:

    https://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

    (@madeincosmos)

    Automattic Happiness Engineer

    Hi @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/

    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:

    https://dominykasgel.com/modify-woocommerce-api-orders-response/

    Cheers!

    madeincosmos

    (@madeincosmos)

    Automattic Happiness Engineer

    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.