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:
Which is a GET function in which the variables are:
purchase_no
order_no
barcode
ordQty
sellPriceAnd ā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)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘API Woocommerce GET function ā SetOrder with variable URL’ is closed to new replies.