Advertisement
Guest User

Untitled

a guest
Mar 4th, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. /* after an order has been processed, we will use the 'woocommerce_thankyou' hook, to add our function, to send the data */
  2. add_action('woocommerce_thankyou', 'wdm_send_order_to_ext');
  3. function wdm_send_order_to_ext( $order_id ){
  4. // get order object and order details
  5. $order = new WC_Order( $order_id );
  6. $email = $order->billing_email;
  7. $phone = $order->billing_phone;
  8. $shipping_type = $order->get_shipping_method();
  9. $shipping_cost = $order->get_total_shipping();
  10.  
  11. // set the address fields
  12. $user_id = $order->user_id;
  13. $address_fields = array('country',
  14. 'title',
  15. 'first_name',
  16. 'last_name',
  17. 'company',
  18. 'address_1',
  19. 'address_2',
  20. 'address_3',
  21. 'address_4',
  22. 'city',
  23. 'state',
  24. 'postcode');
  25.  
  26. $address = array();
  27. if(is_array($address_fields)){
  28. foreach($address_fields as $field){
  29. $address['billing_'.$field] = get_user_meta( $user_id, 'billing_'.$field, true );
  30. $address['shipping_'.$field] = get_user_meta( $user_id, 'shipping_'.$field, true );
  31. }
  32. }
  33.  
  34. // get coupon information (if applicable)
  35. $cps = array();
  36. $cps = $order->get_items( 'coupon' );
  37.  
  38. $coupon = array();
  39. foreach($cps as $cp){
  40. // get coupon titles (and additional details if accepted by the API)
  41. $coupon[] = $cp['name'];
  42. }
  43.  
  44. // get product details
  45. $items = $order->get_items();
  46.  
  47. $item_name = array();
  48. $item_qty = array();
  49. $item_price = array();
  50. $item_sku = array();
  51.  
  52. foreach( $items as $key => $item){
  53. $item_name[] = $item['name'];
  54. $item_qty[] = $item['qty'];
  55. $item_price[] = $item['line_total'];
  56.  
  57. $item_id = $item['product_id'];
  58. $product = new WC_Product($item_id);
  59. $item_sku[] = $product->get_sku();
  60. }
  61.  
  62. /* for online payments, send across the transaction ID/key. If the payment is handled offline, you could send across the order key instead */
  63. $transaction_key = get_post_meta( $order_id, '_transaction_id', true );
  64. $transaction_key = empty($transaction_key) ? $_GET['key'] : $transaction_key;
  65.  
  66. // set the username and password
  67. $api_username = 'testuser';
  68. $api_password = 'testpass';
  69.  
  70. // to test out the API, set $api_mode as ‘sandbox’
  71. $api_mode = 'sandbox';
  72. if($api_mode == 'sandbox'){
  73. // sandbox URL example
  74. $endpoint = "http://sandbox.example.com/";
  75. }
  76. else{
  77. // production URL example
  78. $endpoint = "http://example.com/";
  79. }
  80.  
  81. // setup the data which has to be sent
  82. $data = array(
  83. 'apiuser' => $api_username,
  84. 'apipass' => $api_password,
  85. 'customer_email' => $email,
  86. 'customer_phone' => $phone,
  87. 'bill_firstname' => $address['billing_first_name'],
  88. 'bill_surname' => $address['billing_last_name'],
  89. 'bill_address1' => $address['billing_address_1'],
  90. 'bill_address2' => $address['billing_address_2'],
  91. 'bill_city' => $address['billing_city'],
  92. 'bill_state' => $address['billing_state'],
  93. 'bill_zip' => $address['billing_postcode'],
  94. 'ship_firstname' => $address['shipping_first_name'],
  95. 'ship_surname' => $address['shipping_last_name'],
  96. 'ship_address1' => $address['shipping_address_1'],
  97. 'ship_address2' => $address['shipping_address_2'],
  98. 'ship_city' => $address['shipping_city'],
  99. 'ship_state' => $address['shipping_state'],
  100. 'ship_zip' => $address['shipping_postcode'],
  101. 'shipping_type' => $shipping_type,
  102. 'shipping_cost' => $shipping_cost,
  103. 'item_sku' => implode(',', $item_sku),
  104. 'item_price' => implode(',', $item_price),
  105. 'quantity' => implode(',', $item_qty),
  106. 'transaction_key' => $transaction_key,
  107. 'coupon_code' => implode( ",", $coupon )
  108. );
  109.  
  110. // send API request via cURL
  111. $ch = curl_init();
  112.  
  113. /* 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 */
  114. curl_setopt($ch, CURLOPT_URL, $endpoint."buyitem.php");
  115. curl_setopt($ch, CURLOPT_POST, 1);
  116. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  117. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  118.  
  119. $response = curl_exec ($ch);
  120.  
  121. curl_close ($ch);
  122.  
  123. // the handle response
  124. if (strpos($response,'ERROR') !== false) {
  125. print_r($response);
  126. } else {
  127. // success
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement