1

I need to export woocommerce order data using a POST request to a third-party url.I have this code in functions.php

add_action('woocommerce_thankyou', 'wdm_send_order_to_ext'); 
function wdm_send_order_to_ext ( $order_id ){
   // get order object and order details
   $order = new WC_Order( $order_id ); 
   $email = $order->get_billing_email();
   $phone = $order->get_billing_phone();
   $shipping_type = $order->get_shipping_method();
   $shipping_cost = $order->get_total_shipping();

   // set the address fields
   $user_id = $order->get_user_id();
   $address_fields = array('country',
       'title',
       'first_name',
       'last_name',
       'company',
       'address_1',
       'address_2',
       'address_3',
       'address_4',
       'city',
       'state',
       'postcode');

   $address = array();
   if(is_array($address_fields)){
       foreach($address_fields as $field){
           $address['billing_'.$field] = get_user_meta( $user_id, 'billing_'.$field, true );
           $address['shipping_'.$field] = get_user_meta( $user_id, 'shipping_'.$field, true );
       }
   }
   
   // set the username and password
   $api_username = 'username';
   $api_password = 'password';

       // setup the data which has to be sent
   $data = array(
           'username' => $api_username,
           'password' => $api_password,
       'name1' => $address['billing_first_name'],
           'street' => $address['billing_address_1'],
           'city' => $address['billing_city'],
       'country' => $address['billing_country'],
           'pcode' => $address['billing_postcode'],
           'phone' => $phone,
           'weight' => 4.0,
       'parcel_type' => 'D',
       'num_of_parcel' => 1
       );

           // send API request via cURL
       $curl = curl_init();

       /* set the complete URL, to process the order on the external system. Let’s consider http://example.com/buyitem.php is the URL, which invokes the API */
       curl_setopt($curl, CURLOPT_URL, "https://easyship.si/api/parcel/parcel_import");
       curl_setopt($curl, CURLOPT_POST, 1);
       curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   
       $response = curl_exec ($curl);
   
       curl_close ($curl);
       
}

However, it does not pass the request, the third-party API does not receive anything. WP_DEBUG doesn't see any errors. Tell me where to look, what is wrong?

1 Answers1

0

You can use https://requestbin.com/ as endpoint to see data you are posting and if they are being posted correctly. If it successfully posting it to requestbin endpoint you can see the header and body formats as well. If its not working try debugging line by line. Sorry I wasn't able to add this as comment

KGG
  • 21
  • 1
  • 6